问题描述
我正在使用 malloc
分配内存并在使用后释放它,但在每三次操作中我注意到 malloc
没有分配内存.
I am allocating memory using malloc
and freeing it after using it, but in every third operation I notice that malloc
is not allocating the memory.
谁能告诉我发生了什么...为什么 malloc
不起作用...我应该怎么做才能分配内存?
Can anyone tell me what is happening... why malloc
is not working... what should I do to allocate memory?
发布代码很困难,因为它涉及大量文件......主要是我认为它的内存不足......所以也许我可以以某种方式确定我正在浪费或使用多少内存?
Posting code is difficult as it involves lots and lots of files.. mostly I think that it's running out of memory... so maybe I can determine somehow how much memory I am wasting or using?
推荐答案
正如其他人所说,malloc()
正在返回 NULL
因为您的应用程序内存不足(或者,更准确地说,是虚拟地址空间).
As others have remarked, malloc()
is returning NULL
because your application has run out of memory (or, more precisely, virtual address space).
如果我正确理解了您的描述,那么您已成功运行相同的工作负载两次,但第三次尝试时,您的内存不足.
If I understand your description correctly, you're successfully running the same workload twice, but the third time you try, you're out of memory.
基本上有两件事可能会在这里发生:
There are basically two things that could be happening here:
You're leaking memory. (I see you say that you're freeing the memory you use, but leaking memory accidentally is awfully easy to do.) You can find information on Visual C++'s built-in features for leak detection here.
您正在碎片化内存.随着应用程序开始使用可用 32 位地址空间的很大一部分,碎片已经开始成为一个真正的问题.不幸的是,对于这个问题,并没有真正的一劳永逸的解决方案,但请查看这些 SO 问题以获取更多信息:
You're fragmenting memory. As applications have started using a significant portion of the available 32-bit address space, fragmentation has started to become a real problem. Unfortunately, there isn't really a cut-and-dried solution to this problem, but take a look at these SO questions for more information:
这篇关于malloc不返回内存,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!