fprintf 的问题
我正在用 C 运行模拟,需要存储 3 个 100x100 矩阵 ~1000 次。当我不将数据写入文件时,我的程序运行得很好。但是,当我运行程序并写入数据时,在 250 个时间步左右后出现分段错误。我不明白为什么。
我的保存函数如下所示,
void saveData(Simulation* sim, int number) {
sprintf(pathname_vx, "data/xvel%d.dat", number);
sprintf(pathname_vy, "data/yvel%d.dat", number);
sprintf(pathname_rho, "data/rho%d.dat", number);
FILE* vx_File = fopen(pathname_vx, "w");
FILE* vy_File = fopen(pathname_vy, "w");
FILE* rho_File = fopen(pathname_rho, "w");
int iX, iY;
double ux, uy, rho;
for (iY=0; iY<sim->ly; ++iY) {
for (iX=0; iX<sim->lx; ++iX) {
computeMacros(sim->lattice[iX][iY].fPop, &rho, &ux, &uy);
fprintf(vx_File, "%f ", ux);
fprintf(vy_File, "%f ", uy);
fprintf(rho_File, "%f ", rho);
}
fprintf(vx_File, "\n");
fprintf(vy_File, "\n");
fprintf(rho_File, "\n");
}
fclose(vx_File);
fclose(vx_File);
fclose(vy_File);
}
其中“Simulation”是一个包含晶格(100x100 矩阵)的结构,其中包含 3 个不同的变量“rho”、“ux”、“uy”。 “number”参数只是一个计数变量,用于正确命名文件。
gdb 说了以下内容,但这对我没有多大帮助。
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000010
0x00007fff87c6ebec in __vfprintf ()
我在编程方面没有那么丰富的经验,所以我想有更好的方法将数据写入文件。任何试图澄清我的方法不起作用的尝试都将受到高度赞赏。
谢谢乔恩
I am running a simulation in C, and need to store 3 100x100 matrices ~1000 times. My program runs just fine when I'm not writing the data to file. But when I run my program and write the data, I get a segmentation error after 250 time steps or so. And I don't understand why.
My save function looks like this
void saveData(Simulation* sim, int number) {
sprintf(pathname_vx, "data/xvel%d.dat", number);
sprintf(pathname_vy, "data/yvel%d.dat", number);
sprintf(pathname_rho, "data/rho%d.dat", number);
FILE* vx_File = fopen(pathname_vx, "w");
FILE* vy_File = fopen(pathname_vy, "w");
FILE* rho_File = fopen(pathname_rho, "w");
int iX, iY;
double ux, uy, rho;
for (iY=0; iY<sim->ly; ++iY) {
for (iX=0; iX<sim->lx; ++iX) {
computeMacros(sim->lattice[iX][iY].fPop, &rho, &ux, &uy);
fprintf(vx_File, "%f ", ux);
fprintf(vy_File, "%f ", uy);
fprintf(rho_File, "%f ", rho);
}
fprintf(vx_File, "\n");
fprintf(vy_File, "\n");
fprintf(rho_File, "\n");
}
fclose(vx_File);
fclose(vx_File);
fclose(vy_File);
}
where 'Simulation' is a struct containing a lattice (100x100 matrix) with 3 different variables 'rho', 'ux', 'uy'. The 'number' argument is just a counting variable to name the files correctly.
gdb says the following, but it doesn't help me much.
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000010
0x00007fff87c6ebec in __vfprintf ()
I'm not that experienced in programing, so I guess there are better ways to write data to file. Any attempt to clarify why my approach doesn't work is highly appreciated.
Thanks
jon
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来您关闭了 vx_File 两次,但根本没有关闭 rho_File 。这意味着每次迭代都让 rho_File 打开,因此每次都会用完一个文件描述符。
我猜程序会失败,因为文件描述符用完了。 (由于这种情况发生在第 250 次迭代时,我猜你的限制是 256)。一旦文件描述符用完,fopen() 调用之一将返回 NULL。由于您不检查 fopen() 的返回值,因此当您尝试 fwrite 到 NULL 句柄时将会发生崩溃。
Looks like you're closing vx_File twice, and not closing rho_File at all. This means that you're leaving rho_File open each iteration, and thus using up a file descriptor each time through.
I'd guess the program fails you're running out of file descriptors. (Since this happens on the 250'th iteration, I'd guess your limit is 256). Once you're out of file descriptors, one of the fopen() calls will return NULL. Since you don't check the return value of fopen(), the crash will occur when you attempt to fwrite to a NULL handle.
看起来像 NULL 指针取消引用。您需要检查 fopen() 的结果以确保它成功(非 NULL 结果)。
Looks like a NULL pointer dereference. You need to check the result of fopen() to make sure it succeeded (non-NULL result).
当您创建那一千个 100x100 矩阵(或任何正在发生的事情)时,也许您会耗尽内存。然后,您可能会得到一个不完整的
sim->lattice
,其中可能包含NULL
指针。您检查
malloc()
调用是否成功?如果它们无法分配内存,则会返回NULL
。Maybe you run out of memory when you are creating those thousand 100x100 matrices (or whatever is exactly happening). You could then end up with a incomplete
sim->lattice
that might containNULL
pointers.Do you check if you
malloc()
calls succeed? If they can't allocate memory they returnNULL
.