c++ - C++ MPI Iprobe unitialized local variable 'buf' used
问题描述
I'm new to C++ and MPI. Got the task and read a lot. I`m still confident that I wrote everything correctly, but still unable to execute without any errors. My code:
#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;
}
Errors:
Error C4700 uninitialized local variable 'buf' used
I don't get why it wants it to be initialized. I gave the memory space and just want to fill it further. It seems I don`t unserstand some C++ simple operation or smth. Initializing something like
int **buf = nullptr;
Also tried:
buf[i] = new int[count * sizeof(int)];
didn`t make any difference. Please give me a hint.
解决方案
好吧,我认为您需要检查计数值。
示例(提示(?))
count = 0;
再试一次
推荐阅读
- excel - 特定 VBA 函数(例如 SendKeys)的潜在 Google 脚本等效项
- sql - 为什么我收到 TADOQuery 选择语句错误?
- sql - SQL 这个查询的另一种解决方案?
- python - 查找python打印功能
- c# - 将 DateTime 转换为月份数时出现 ASP.NET MVC ToString 错误
- c# - 如何每天像(1PM,2Pm,3Pm,4Pm,5Pm)每天5次调用AVplayer?
- android - 如何修复 Wordpress 应用程序在更新 5.2.1 后停止工作?
- javascript - 为什么 JS .focus() 只能在 50ms 或更长的 setTimeout 内工作
- javascript - 仅使用数组检查给定半径内的点
- asp.net - 如何在没有 JavaScript 或没有任何 JavaScript 表单的情况下更新字段