简体   繁体   English

新操作员无法分配内存

[英]new operator unable to allocate memory

In my C++ program I am required to handle large amount of information. 在我的C ++程序中,我需要处理大量信息。 I'm storing this large information in a structure. 我将这些大信息存储在一个结构中。 the structure is.. 结构是......

struct xlsmain
{
    struct sub_parts_of_interface sub_parts[200];
    string name;
    int no_of_subparts;
};

struct sub_parts_of_interface
{
    struct pio_subparts pio[10];
    string name_of_interface;
    string direction; 
    string partition;
    int no_of_ports;
};

struct pio_subparts
{
    struct corners corner[32]; 
    string pio_name;     
};

struct corners
{
    struct file_structure merging_files[50];
    string corner_name;
    int merginf_files_num;
};

struct file_structure
{
    string file_name;
    string file_type;
    struct reportspecs report[20];
};

I'm using new operator to allocate memory to it 我正在使用new运算符为它分配内存

struct xlsmain *interface = new xlsmain[60];

On running the program std::bad_alloc error is shown. 在运行程序时,会显示std::bad_alloc error Plese help! 请帮忙!

You are trying to allocate 60 xlsmain which contain 12,000 sub_parts_of_interface which contain 120,000 pio_subparts which contain 3,840,000 corners which contain 192,000,000 file_structure which contain 3,840,000,000 reportspecs , and you have not shown the definition of reportspecs . 您正在尝试分配60个xlsmain ,其中包含12,000个sub_parts_of_interface ,其中包含120,000个pio_subparts ,其中包含3,840,000个包含192,000,000个file_structure corners ,其中包含3,840,000,000个reportspecs ,并且您没有显示reportspecs的定义。 So you are trying to allocate 3.8 billion of something and are running out of memory. 因此,您正在尝试分配38亿的东西并且内存不足。

That is because the system you are running on does not have enough memory to hold the objects you are trying to allocate. 这是因为您运行的系统没有足够的内存来容纳您尝试分配的对象。

If those structures contain arrays because the structures might hold those objects, but they usually do not contain the maximums, then get rid of the arrays and replace them std::vector (in C++) or pointers (in C or C++). 如果这些结构包含数组,因为结构可能包含这些对象,但它们通常不包含最大值,那么去除数组并将它们替换为std::vector (在C ++中)或指针(在C或C ++中)。 With either method, you will use just space for the actual objects needed plus some accounting overhead, instead of using space for the maximum theoretically possible. 无论使用哪种方法,您都只需要空间用于所需的实际对象以及一些会计开销,而不是在理论上可以使用空间。 std::vector allows you to add elements as needed, and the std::vector application will take care of allocating the needed memory. std::vector允许您根据需要添加元素, std::vector应用程序将负责分配所需的内存。 With pointers, you would have to allocate and free the space yourself. 使用指针,您必须自己分配和释放空间。

It's simple enough. 这很简单。 You haven't got enough memory. 你没有足够的记忆力。 Either buy some more memory or redesign your code. 要么购买更多内存,要么重新设计代码。 I would suggest that your replace your arrays with zero length vectors. 我建议用零长度向量替换你的数组。 That way you only need to grow the vectors to the size you actually need, instead of having a fixed size always. 这样你只需要将矢量增长到你实际需要的大小,而不是总是固定大小。 That will use less memory and be more flexible as well. 这将使用更少的内存并且也更灵活。 For instance 例如

  struct pio_subparts
   {
    vector<corners> corner; 
    string pio_name;     
   };

More precise advice isn't possible without knowing exactly what you are trying to do. 如果不确切知道自己要做什么,就无法提供更精确的建议。

@H2CO3 is right. @ H2CO3是对的。

In pure C use something like this: http://www.elook.org/programming/c/malloc.html 在纯C中使用类似这样的东西: http//www.elook.org/programming/c/malloc.html

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

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