内存分配问题 C/Cpp Windows 严重错误

发布于 2024-08-26 17:21:51 字数 1738 浏览 10 评论 0原文

我有一段代码需要从 C“翻译”为 Cpp,但我无法理解,问题出在哪里。有一部分崩溃了(Windows 严重错误发送/不发送):

 nDim = sizeMax*(sizeMax+1)/2;
 printf("nDim  = %d sizeMax = %d\n",nDim,sizeMax);
 hamilt = (double*)malloc(nDim*sizeof(double));
 printf("End hamilt alloc. %d allocated\n",(nDim*sizeof(double)));
 transProb = (double*)malloc(sizeMax*sizeMax*sizeof(double));
 printf("End transProb alloc. %d allocated\n",(sizeMax*sizeMax*sizeof(double)));
 eValues = (double*)malloc(sizeMax*sizeof(double));
 printf("eValues allocated. %d allocated\n",(sizeMax*sizeof(double)));
    eVectors  = (double**)malloc(sizeMax*sizeof(double*));
 printf("eVectors allocated. %d allocated\n",(sizeMax*sizeof(double*)));
 if(eVectors) for(i=0;i<sizeMax;i++) {
                 eVectors[i] = (double*)malloc(sizeMax*sizeof(double));
                 printf("eVectors %d-th element allocated. %d allocated\n",i,(sizeMax*sizeof(double)));
                 }
 eValuesPrev = (double*)malloc(sizeMax*sizeof(double));
 printf("eValuesPrev allocated. %d allocated\n",(sizeMax*sizeof(double)));
 eVectorsPrev  = (double**)malloc(sizeMax*sizeof(double*));
 printf("eVectorsPrev allocated. %d allocated\n",(sizeMax*sizeof(double*)));
 if(eVectorsPrev) for(i=0;i<sizeMax;i++) {
                     eVectorsPrev[i] = (double*)malloc(sizeMax*sizeof(double));
                     printf("eVectorsPrev %d-th element allocated. %d allocated\n",i,(sizeMax*sizeof(double)));
                     }

日志:

nDim  = 2485 sizeMax = 70
End hamilt alloc. 19880 allocated
End transProb alloc. 39200 allocated
eValues allocated. 560 allocated
eVectors allocated. 280 allocated 

因此它在分配循环开始时崩溃。如果我删除这个循环,它会在下一行分配时崩溃。这是否意味着像这样的数字我没有足够的内存?

谢谢。

I have a code that need to be "translated" from C to Cpp, and i cant understand, where's a problem. There is the part, where it crashes (windows critical error send/dontSend):

 nDim = sizeMax*(sizeMax+1)/2;
 printf("nDim  = %d sizeMax = %d\n",nDim,sizeMax);
 hamilt = (double*)malloc(nDim*sizeof(double));
 printf("End hamilt alloc. %d allocated\n",(nDim*sizeof(double)));
 transProb = (double*)malloc(sizeMax*sizeMax*sizeof(double));
 printf("End transProb alloc. %d allocated\n",(sizeMax*sizeMax*sizeof(double)));
 eValues = (double*)malloc(sizeMax*sizeof(double));
 printf("eValues allocated. %d allocated\n",(sizeMax*sizeof(double)));
    eVectors  = (double**)malloc(sizeMax*sizeof(double*));
 printf("eVectors allocated. %d allocated\n",(sizeMax*sizeof(double*)));
 if(eVectors) for(i=0;i<sizeMax;i++) {
                 eVectors[i] = (double*)malloc(sizeMax*sizeof(double));
                 printf("eVectors %d-th element allocated. %d allocated\n",i,(sizeMax*sizeof(double)));
                 }
 eValuesPrev = (double*)malloc(sizeMax*sizeof(double));
 printf("eValuesPrev allocated. %d allocated\n",(sizeMax*sizeof(double)));
 eVectorsPrev  = (double**)malloc(sizeMax*sizeof(double*));
 printf("eVectorsPrev allocated. %d allocated\n",(sizeMax*sizeof(double*)));
 if(eVectorsPrev) for(i=0;i<sizeMax;i++) {
                     eVectorsPrev[i] = (double*)malloc(sizeMax*sizeof(double));
                     printf("eVectorsPrev %d-th element allocated. %d allocated\n",i,(sizeMax*sizeof(double)));
                     }

Log:

nDim  = 2485 sizeMax = 70
End hamilt alloc. 19880 allocated
End transProb alloc. 39200 allocated
eValues allocated. 560 allocated
eVectors allocated. 280 allocated 

So it crashes at the start of the loop of allocation. If i delete this loop it crashes at the next line of allocation. Does it mean that with the numbers like this i have not enough memory??

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

末が日狂欢 2024-09-02 17:21:51

在我的机器上,该程序编译、执行时没有错误,并且在通过 valgrind 运行时报告没有内存问题。除非您在小型嵌入式系统上运行,否则您的问题很可能是此代码之外的问题,因为此程序分配的内存总量小于 140 KiB。

此外,当malloc失败时,它不会崩溃,而是返回NULL。此代码正确检查 eVectorsPrev 是否为 NULL,因此此处不应存在 NULL 取消引用问题。

On my machine, this program compiles, executes without error and reports no memory problems when run through valgrind. Unless you are running on a small embedded system, your problem is likely something external to this code, because the total amount of memory allocated by this program is less than 140 KiB.

Besides, when malloc fails, it doesn't crash, it returns NULL. This code properly checks for eVectorsPrev being NULL, so there should be no NULL dereferencing problems here.

小糖芽 2024-09-02 17:21:51

您可能不想分配太多内存。其他一些代码可能损坏了堆,并且几乎可以咬住任何之后代码中的任意点。请参阅链接以获取调试帮助。

如果在无缺陷程序中内存不足,malloc 将返回错误指示。

You are probably not trying to allocate too much memory. Some other code has likely corrupted the heap and that can bite at just about any arbitrary point in the code afterwards. See the link for debugging aids.

If you were out of memory in a defect free program, malloc would return an error indication.

﹏雨一样淡蓝的深情 2024-09-02 17:21:51

什么是sizeMax?我没有看到声明,请修正格式。附注...您盲目地调用 malloc 而不检查它是否有效...这是一个危险的假设 - 永远不要假设有足够的可用内存,因为堆可能会变得非常碎片化并且当对碎片堆中非常小的对象调用malloc时可能会因为没有足够的内存而失败......

What is sizeMax? I do not see the declaration, please fix the formatting. A side note... you are blindingly calling malloc without checking if it worked or not...that is a dangerous assumption - never assume there is plenty of memory available as the heap could become very fragmented and when a call to malloc for a very small object in a fragmented heap could fail as there is not enough memory...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文