- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用为解决热问题而编写的 C 程序时,我收到以下错误:
说
In function 'main':
warning: dereferencing 'void *' pointer [enabled by default]
if (!fp) *NULL;
该程序的目的是:
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <unistd.h>
#include <mpi.h>
#define COLS 1000
#define ROWS 1000
#define WHITE "15 15 15 "
#define RED "15 00 00 "
#define ORANGE "15 05 00 "
#define YELLOW "15 10 00 "
#define LTGREEN "00 13 00 "
#define GREEN "05 10 00 "
#define LTBLUE "00 05 10 "
#define BLUE "00 00 10 "
#define DARKTEAL "00 05 05 "
#define BROWN "03 03 00 "
#define BLACK "00 00 00 "
void copyNewToOld(float grid_a[ROWS][COLS], float grid_b[ROWS][COLS], int x_lower, int x_upper) {
int x, y;
for (x = x_lower; x < x_upper; ++x) {
for (y = 0; y < COLS; ++y) {
grid_b[x][y] = grid_a[x][y];
}
}
}
void calculateNew(
float grid_a[ROWS][COLS],
float grid_b[ROWS][COLS],
int x_lower,
int x_upper
) {
// Adjust bounds
if (x_lower == 0) x_lower = 1;
if (x_upper == ROWS) x_upper = ROWS - 1;
int x, y;
for (x = x_lower; x < x_upper - 1; ++x) {
for (y = 1; y < COLS - 1; ++y) {
grid_a[x][y] = 0.25 * (grid_b[x-1][y] + grid_b[x+1][y] + grid_b[x][y-1] + grid_b[x][y+1]);
}
}
}
void printGridtoFile(FILE* fp, float grid[ROWS][COLS], int x_lower, int x_upper) {
int x, y;
for (x = x_lower; x < x_upper; ++x) {
for (y = 0; y < COLS; ++y) {
if (grid[x][y] > 250) {
fprintf(fp, "%s ", RED );
} else if (grid[x][y] > 180) {
fprintf(fp, "%s ", ORANGE );
} else if (grid[x][y] > 120) {
fprintf(fp, "%s ", YELLOW );
} else if (grid[x][y] > 80) {
fprintf(fp, "%s ", LTGREEN );
} else if (grid[x][y] > 60) {
fprintf(fp, "%s ", GREEN );
} else if (grid[x][y] > 50) {
fprintf(fp, "%s ", LTBLUE );
} else if (grid[x][y] > 40) {
fprintf(fp, "%s ", BLUE );
} else if (grid[x][y] > 30) {
fprintf(fp, "%s ", DARKTEAL );
} else if (grid[x][y] > 20) {
fprintf(fp, "%s ", BROWN );
} else {
fprintf(fp, "%s ", BLACK );
}
}
fprintf(fp, "\n");
}
}
int main(int argc, char **argv) {
int h, w, cycles, heat;
float grid_a[ROWS][COLS];
float grid_b[ROWS][COLS];
if (argc != 2) {
printf("Usage: ./program <number of timestamps>\n");
exit(0);
}
cycles = atoi(argv[1]);
MPI_Init(NULL, NULL);
int mpi_size;
MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
int mpi_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
int x_lower = mpi_rank * ROWS / mpi_size;
int x_upper = (mpi_rank+1) * ROWS / mpi_size;
/*fprintf(stderr, "CPU=%d/%d x_lower=%d x_upper=%d\n", mpi_rank, mpi_size, x_lower, x_upper);*/
for (h = x_lower; h < x_upper; ++h) {
for (w = 0; w < COLS; ++w) {
grid_a[w][h] = 20;
}
}
for (heat = 299; heat < 700; ++heat) {
grid_a[0][heat] = 300;
}
for (cycles; cycles > 0; --cycles) {
/*fprintf(stderr, "[%d] Cycle=%d\n", mpi_rank, cycles);*/
copyNewToOld(grid_a, grid_b, x_lower, x_upper);
if (mpi_rank != 0) MPI_Sendrecv(
grid_a[x_lower-1], // sendbuf
COLS, // sendcount
MPI_FLOAT, // sendtype
mpi_rank-1, // dest
0, // sendtag
grid_b[x_lower-1], // recvbuf
COLS, // recvcount
MPI_FLOAT, // recvtype
mpi_rank-1, // source
0, // recvtag
MPI_COMM_WORLD, // communicator
MPI_STATUS_IGNORE // status
);
if (mpi_rank+1 != mpi_size) MPI_Sendrecv(
grid_a[x_upper+1], // sendbuf
COLS, // sendcount
MPI_FLOAT, // sendtype
mpi_rank+1, // dest
0, // sendtag
grid_b[x_upper+1], // recvbuf
COLS, // recvcount
MPI_FLOAT, // recvtype
mpi_rank+1, // source
0, // recvtag
MPI_COMM_WORLD, // communicator
MPI_STATUS_IGNORE // status
);
calculateNew(grid_a, grid_b, x_lower, x_upper);
}
FILE* fp;
if (mpi_rank == 0) {
fp = fopen("c.pnm", "w");
fprintf(fp, "P3\n%d %d\n15\n", COLS, ROWS);
fclose(fp);
}
char dummy = 1;
if (mpi_rank != 0) MPI_Recv(
&dummy, // buf
1, // count
MPI_BYTE, // type
mpi_rank-1, // source
0, // tag
MPI_COMM_WORLD, // communicator
MPI_STATUS_IGNORE // status
);
/*fprintf(stderr, "[%d] Open file\n", mpi_rank);*/
fp = fopen("c.pnm", "a");
if (!fp) *NULL;
/*fprintf(stderr, "[%d] Start printing\n", mpi_rank);*/
printGridtoFile(fp, grid_a, x_lower, x_upper);
/*fprintf(stderr, "[%d] Done printing\n", mpi_rank);*/
fclose(fp);
if (1) MPI_Send(
&dummy, // buf
1, // count
MPI_BYTE, // type
(mpi_rank+1) % mpi_size, // source
0, // tag
MPI_COMM_WORLD // communicator
);
if (mpi_rank == 0) {
MPI_Recv(
&fp, // buf
1, // count
MPI_UNSIGNED_LONG, // type
mpi_size-1, // source
0, // tag
MPI_COMM_WORLD, // communicator
MPI_STATUS_IGNORE // status
);
system("convert c.pnm c.png");
}
MPI_Finalize();
return 0;
}
任何有关此问题的帮助都会很棒,我对 C 相当陌生。希望使用此代码并在具有多个不同进程的 Bridges super 计算机上进行 mpirun。
最佳答案
问题是这段代码:
/*fprintf(stderr, "[%d] Open file\n", mpi_rank);*/
fp = fopen("c.pnm", "a");
if (!fp) *NULL;
/*fprintf(stderr, "[%d] Start printing\n", mpi_rank);*/
printGridtoFile(fp, grid_a, x_lower, x_upper);
/*fprintf(stderr, "[%d] Done printing\n", mpi_rank);*/
fclose(fp);
您正在尝试取消引用 NULL
,根据 C 标准,这会导致未定义的行为。在您的情况下,这很可能不会造成任何不良影响,因为该表达式没有副作用,并且编译器可能会优化它。您使用的编译器足够聪明,可以发现这个潜在的问题,并在您犯错误时向您发出警告。
就您而言,我猜您只是想在打开文件时处理失败。这是您更好的做法:
#include <errno.h>
...
/*fprintf(stderr, "[%d] Open file\n", mpi_rank);*/
errno = 0;
fp = fopen("c.pnm", "a");
if (NULL == fp)
{
fprintf(stderr, "Failed to open file due to %d\n", errno);
exit(errno);
}
/*fprintf(stderr, "[%d] Start printing\n", mpi_rank);*/
printGridtoFile(fp, grid_a, x_lower, x_upper);
/*fprintf(stderr, "[%d] Done printing\n", mpi_rank);*/
fclose(fp);
关于c - 警告 : dereferencing 'void *' pointer error - C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59097365/
作为作业的一部分,我正在尝试创建一个用户级线程库,如 pthreads。 为了处理线程之间的上下文切换,我使用了“swapcontext”函数。在使用它之前,我必须使用“makecontext”函数创
我是一名初级 C++ 程序员,我正在 Linux 机器上编程。 我遇到了这个错误: cannot convert ‘void* (Network::*)(void*)’ to ‘void* (*)(v
我知道,例如 void *(*myFuncName)(void*) 是一个函数指针,它接受并返回 void*。 这是一个有两个参数的指针吗?void 指针是该类型的另一个返回 void* 和 void
所以我被告知它们彼此几乎相同 void function1 (void(func)(int), int arg){ func(arg); } void function2 (void(*fun
我目前正在 GNU Radio 上开发一个 bloc,我想使用一个线程。该线程用于从 UDP 套接字获取数据,因此我可以在我的 GNU Radio 集团中使用它。 “一般工作”功能是执行所有信号和数据
我正在尝试在主函数中创建一个线程并通过我的线程调用另一个类的函数。 在 main.cpp 中: SocketHandler *callserver; pthread_t thread1; pthrea
我正在使用pthread 为我自己实现线程类。所以,我创建了 Thread 类如下: class Thread { public: Thread() { } virtual void*
我收到上述警告并理解它,但就是不知道如何解决它。我的代码在下面,但基本上我所做的是在结构中存储一个函数指针,并在我从 main.c 调用的另一个函数中初始化该结构。当我将代码与默认函数(即 free(
在我的 android 应用程序中,我在 doInBackground 中执行一些操作通过扩展 AsyncTask类(class)。 (我在这个类中执行任何 UI 都没用) 这是正确使用 AsyncT
我在 GNU 编译器集合中使用 C。所以我需要将函数指针传递给一个函数。现在有两种我想要处理的可接受的函数指针原型(prototype): void function(void); 和 void fu
我正在尝试使用“CameraManager”类创建一个新线程,但出现以下错误: cannot convert '*void(CameraManager:: * )(void*) to void*( *
我想构建一个可以隐藏线程创建的“IThread”类。子类实现“ThreadMain”方法并使其自动调用,如下所示: class IThread { public: void BeginThre
我不明白什么 void (**)(void *, const char *) /* ^^ why are there 2 asterisks here? 意思是,它是一个指向函数的指针,但我失败
我必须将“risposta”类型的参数“r”发送到函数 RispostaServer。编译器给我:invalid conversion void*(*)() to void*(*)(void*) 这是
所以我目前正在使用,或者至少正在尝试编写一个利用 this C pthread threadpool library. 的程序 值得注意的是 thpool.h 中的以下函数: int thpool_a
我正在尝试使用 void* 指针将不同的对象存储在一个全局表中。问题是如何取回 void* 对象。如果我有一个公共(public)基类,比如 Object ,我总是可以将 void* 指针存储为 Ob
我是一名 C 程序员(在 linux 上),但现在我有一个关于 C++ 的项目,并且有一个问题。 这里是示例代码 g_action.sa_sigaction = (void(*)(int,siginf
class Scoreget{ private: //some variables public: Scoreget(){ //
这个问题在这里已经有了答案: Is there a difference between foo(void) and foo() in C++ or C? (4 个答案) func() vs fun
我正在尝试使用 SDL 和 SDL_Mixer 为音频创建一个 C++ 应用程序,并且正在尝试遵循 this教程。但是,使用 SDL_Mixer 的 Mix_HookMusicFinished() 不
我是一名优秀的程序员,十分优秀!