简体   繁体   English

malloc 无法分配内存

[英]malloc is failing to allocate memory

I'm writing a C Code to solve Euler equations.我正在编写一个 C 代码来求解欧拉方程。 My code works perfectly fine on the cluster but not on my pc.我的代码在集群上工作得很好,但在我的电脑上却没有。 Seems to be an issue with malloc().似乎是 malloc() 的问题。 It is not being able to allocate the requested memory and fails.它无法分配请求的内存并失败。

How do i make it work?我如何使它工作? is it something to do with Defragmentation?它与碎片整理有关吗? But the system settings show (0% Defragmented).但系统设置显示(0% 碎片整理)。

Just including a part of malloc() code here.这里只包含一部分 malloc() 代码。

double **u, **rho_u, **rho,    
int Size = 1000;
u = (double**)malloc(Size*sizeof(double*));
for(i=0;i<=Size;i++)
    u[i] = (double*)malloc(Size*sizeof(double));

rho_u = (double**)malloc(Size*sizeof(double*));
for(i=0;i<=Size;i++)
    rho_u[i] = (double*)malloc(Size*sizeof(double));

You probably corrupt your heap here:你可能在这里破坏了你的堆:

for(i=0;i<=Size;i++)
    u[i] = (double*)malloc(Size*sizeof(double));

You assign 1001 pointers, but only have 1000 allocated.您分配了 1001 个指针,但只分配了 1000 个。 Correct version:正确版本:

for(i=0;i<Size;i++)
    u[i] = (double*)malloc(Size*sizeof(double));

Same for the second loop.第二个循环相同。

Read carefully the documentation of malloc .仔细阅读malloc文档 It can fail, and when it does fail, malloc returns NULL (and the failure reason is given by errno which you often display using perror ).可能会失败,当它失败时, malloc返回NULL (失败原因由errno给出,您经常使用perror显示)。

So you should test against failure of malloc .所以你应该测试malloc失败。 The typical code is at least:典型的代码至少是:

u = (double**)malloc(Size*sizeof(double*));
if (u==NULL) { perror ("malloc u"); exit(EXIT_FAILURE); };

and likewise for your rho_u and every rho_u[i]同样对于您的rho_u和每个rho_u[i]

Some operating systems may provide memory overcomitment .某些操作系统可能会提供内存过量使用 It is a feature I dislike.这是我不喜欢的功能。

Consider initializing every memory zone entirely.考虑完全初始化每个内存区域。 And using memory outside of a valid memory zone (or a valid address) is undefined behavior (and your program have one, noticed by Ctx's answer ).并且在有效内存区域(或有效地址)之外使用内存是未定义的行为(并且您的程序有一个, Ctx 的回答注意到了这一点)。 Be scared .害怕

I also recommend using valgrind .我还建议使用valgrind It is a very handy tool to hunt memory related bugs, and could have detected yours.这是一个非常方便的工具,用于寻找与内存相关的错误,并且可以检测到您的错误。

Observation :观察:

  • Avoid typecasting malloc() , Read Do I cast the result of malloc?避免类型转换malloc() ,阅读我是否转换了malloc() 的结果?
  • Check the return value of malloc() & do proper error handling.检查malloc()的返回值并进行适当的错误处理。
  • Change loop condition from i<=Size to i<Size as it cause buffer overrun since earlier memory was allocated only for Size rows not size+1 rows.将循环条件从i<=Size更改为i<Size因为它会导致缓冲区溢出,因为较早的内存仅分配给Size行而不是size+1行。

Try this version :试试这个版本:

int Size = 1000;
double **u = malloc(Size * sizeof(*u)); /* typecasting is not needed */
if(u == NULL) {
   /* @TODO error handling */
}
for(i=0;i<Size;i++) { /* loop should rotate only 1000 times not 1001 times */
    u[i] = malloc(Size * sizeof(**u));
    if(u[i] == NULL) {
         /* @TODO error handling */
    }
}

Similarly for rho_u and rho .同样对于rho_urho

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM