内存分配问题 C/Cpp Windows 严重错误
我有一段代码需要从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在我的机器上,该程序编译、执行时没有错误,并且在通过 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 foreVectorsPrev
being NULL, so there should be no NULL dereferencing problems here.您可能不想分配太多内存。其他一些代码可能损坏了堆,并且几乎可以咬住任何之后代码中的任意点。请参阅链接以获取调试帮助。
如果在无缺陷程序中内存不足,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.
什么是
sizeMax
?我没有看到声明,请修正格式。附注...您盲目地调用malloc
而不检查它是否有效...这是一个危险的假设 - 永远不要假设有足够的可用内存,因为堆可能会变得非常碎片化并且当对碎片堆中非常小的对象调用malloc
时可能会因为没有足够的内存而失败......What is
sizeMax
? I do not see the declaration, please fix the formatting. A side note... you are blindingly callingmalloc
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 tomalloc
for a very small object in a fragmented heap could fail as there is not enough memory...