简体   繁体   English

使用指针的内存分配失败

[英]Memory allocation failure with pointers

I have searched about the famous error of "std::bad_alloc" but I couldn't find any case like mine. 我搜索了有关“std :: bad_alloc”的着名错误,但我找不到像我这样的情况。 I've implemented a code in c++ (to be accurate: running in MinGW with Eclipse in Win 7 Prof.) and the code runs great for small number of class instances, but when the number of class instances reaches over 10509, the error from windows appears and then eclipse shows the 'std::bad_alloc' error. 我已经在c ++中实现了一个代码(准确地说:在Win7 Prof.中使用Eclipse在MinGW中运行)并且代码对于少量类实例运行良好,但是当类实例的数量达到10509以上时,错误来自windows出现然后eclipse显示'std :: bad_alloc'错误。 I am using "new" to build the new class instances and each class is needed two pointers and three variables of types "long int" , "string" and "int". 我使用“new”来构建新的类实例,每个类需要两个指针和三个类型为“long int”,“string”和“int”的变量。 I can not deconstruct any instances build before because I need them as nodes later on to be evaluated on some other part of program. 我无法解构之前构建的任何实例,因为我以后需要它们作为节点在程序的其他部分进行评估。 But, I have added class deconstructor anyway to class definitions and they set properly for sure. 但是,无论如何,我已经将类解构器添加到类定义中,并且它们设置正确。 I am sure about when and by which command the program stops, which is exactly after constructing 10509 class instances right when trying to add the 10510th class instance. 我确定程序何时以及通过哪个命令停止,这正是在尝试添加第10510个类实例时正确构造10509个类实例之后。

So I am wondering if it is possible to extend the reserved memory to allocate new class instances or not? 所以我想知道是否可以扩展保留内存以分配新的类实例?

The code is too long to be pasted here. 代码太长,无法粘贴在这里。 Thank you for any help! 感谢您的任何帮助!

Firstly, the dynamic memory in which new works typically occupies all remaining available memory in your process. 首先,动态内存中new作品通常占用进程中所有剩余的可用内存。 There's no way to extend it further. 没有办法进一步扩展它。 How much dynamic memory you had initially depends on how much non-dynamic data your program has. 您最初拥有多少动态内存取决于您的程序具有多少非动态数据。 There's no way to say without knowing more about your program. 如果不了解更多关于你的计划的话,没有办法说出来。 By reducing the size of non-dynamic data you might effectively "extend" what will be available as dynamic memory. 通过减少非动态数据的大小,您可以有效地“扩展”可用作动态内存的内容。

Secondly, on platforms with virtual memory it is not really possible to "run out of memory" (assuming you have a healthy swap file). 其次,在具有虚拟内存的平台上,实际上不可能“耗尽内存”(假设您有一个健康的交换文件)。 But you can run out of process address space . 但是你可以用完进程地址空间 Again, if you run out of address space after allocating only 10509 objects, something else must be wrong. 同样,如果在仅分配10509对象后耗尽地址空间,则必须有其他错误。

Thirdly, 10509 looks like a really low number. 第三, 10509看起来非常低。 So, if you run out of memory after allocating so few objects it probably means that the objects themselves are rather large. 因此,如果在分配如此少的对象后内存不足,则可能意味着对象本身相当大。 How large are your objects? 你的物品有多大?

Fourthly, dynamic memory allocation errors might be triggered by heap corruption, meaning that allocation will fail even if there's technically plenty of free memory still left available. 第四,动态内存分配错误可能由堆损坏触发,这意味着即使技术上仍有足够的可用内存,分配也会失败。 There's no way to say without knowing more about your code. 如果不了解更多关于代码的内容,就没有办法说出来。

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

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