提问人:Юрий Дзбановский 提问时间:10/10/2021 最后编辑:double-beepЮрий Дзбановский 更新时间:11/18/2021 访问量:107
使用 C++ MPI Iprobe 单元化局部变量“buf”
C++ MPI Iprobe unitialized local variable 'buf' used
问:
我是 C++ 和 MPI 的新手。接到任务并阅读了很多。我仍然相信我正确地编写了所有内容,但仍然无法执行而不会出现任何错误。我的代码:
#include <iostream>
#include <mpi.h>
using namespace std;
int main() {
int myid, numprocs, **buf, source, i;
int message[3] = { 0, 1, 2 };
int myrank, data = 2002, count, TAG = 0;
MPI_Status status;
MPI_Init(NULL, NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
if (myrank == 0) {
MPI_Send(&data, 1, MPI_INT, 2, TAG, MPI_COMM_WORLD);
}
else if (myrank == 1) {
MPI_Send(&message, 3, MPI_INT, 2, TAG, MPI_COMM_WORLD);
}
else {
MPI_Probe(MPI_ANY_SOURCE, TAG, MPI_COMM_WORLD, &status);
source = status.MPI_SOURCE;
MPI_Get_count(&status, MPI_INT, &count);
for (i = 0; i < count; i++) {
//buf[i] = new int[count * sizeof(int)];
buf[i] = (int *)malloc(count * sizeof(int));
}
MPI_Recv(&buf[0], count, MPI_INT, source, TAG, MPI_COMM_WORLD, &status);
for (i = 0; i < count; i++) {
printf("received: %d\n", buf[i]);
}
}
MPI_Finalize();
return 0;
}
错误:
Error C4700 uninitialized local variable 'buf' used
我不明白它为什么要初始化它。我给了内存空间,只想进一步填充它。似乎我没有解开一些 C++ 简单的操作或 smth。初始化类似
int **buf = nullptr;
还尝试过:
buf[i] = new int[count * sizeof(int)];
没有任何区别。请给我一个提示。
答:
0赞
Lina
11/18/2021
#1
好吧,我认为您需要检查计数值。
示例 (hint(?))
count = 0;
再试一次
评论
0赞
overseas
11/19/2021
这既不是警告所描述的,也不是程序中的错误,因为计数是通过 GetCount 启动的。这是关于 buf 未初始化,正如评论已经暗示的那样。
评论
buf
确实是未初始化的。int * buf
buf = (int *)malloc(count * sizeof(int)
MPI_Recv(buf, ...)
Error C4703 potentially uninitialized local pointer variable 'buf' used
s inside of "else", but how could I do it earlier if I haven
buf = new int[count * sizeof(int)];
MPI_Recv(buf, count, MPI_INT, source, TAG, MPI_COMM_WORLD, &status);