简体   繁体   English

结构消耗太多内存

[英]Struct consumes too much memory

I'm doing a school assignment dealing with Structs in C++. 我正在做学校作业,处理C ++中的Structs。 The assignment does not allow the use of STL or new / delete . 该分配不允许使用STL或new / delete

I'm representing items in a magazine. 我代表杂志上的物品。

struct PLACE {
    int itemCount;
    int etiquette[2];
};

struct SHELF {
    int placesCount;
    PLACE places[128];
};

struct RACK {
    int shelvesCount;
    SHELF shelves[128]; 
};

struct MAG {
    int racksCount;
    RACK racks[5];
};

Now, when I run the following code, it works fine, but if I change the length of racks array to something like 20, I get a runtime error (code.exe has stopped working). 现在,当我运行以下代码时,它可以正常工作,但是,如果将racks数组的长度更改为20左右,则会出现运行时错误(code.exe停止工作)。

PLACE place1 = {1, {1,2}};
SHELF shelf1 = {2, {place1}};   
RACK rack1 = {3, {shelf1}};
MAG mag1 = {1, {rack1}};

It seems that I'm exceeding some sort of memory? 似乎我超出了某种记忆力? I would have thought that unlikely since when I calculated it, (when racks length is 5) I'm only using (((12 * 128) + 4) * 128 + 4) * 5 + 4 bytes, which is around 1MB. 我本来以为这不太可能,因为我在计算时(机架长度为5时)仅使用((((12 * 128)+ 4)* 128 + 4)* 5 + 4个字节,大约1MB。

EDIT:

I asked my instructor for some clarification on this. 我要求我的教练对此做一些澄清。 He told me that it's possible than my heap is exceeded, not stack , because: (quote) 他告诉我,有可能超过我的 ,而不是堆栈 ,因为:(quote)

Stack deals with function calls and stack exceeded could be caused by for example recursion, while here we're dealing with large data, so it's rather the heap being exceeded. 堆栈处理函数调用,而超出堆栈可能是由例如递归引起的,而此处我们正在处理大型数据,因此它是超出了堆。

He also told me that it's possible that my computer just has a lower limit (around 4MB), while the server that our code is being checked on has been allocated 1GB to handle this much data. 他还告诉我,我的计算机可能只有一个较低的限制(大约4MB),而正在检查我们的代码的服务器已分配了1GB的空间来处理大量数据。

Don't know if this is true though, because from what I read online, both stack and heap can be allocated variables and stack isn't reserved purely for function calls, but also for variables inside of these functions... 但是不知道这是否是正确的,因为从我在线阅读的内容来看,堆栈和堆都可以分配变量,而堆栈不仅仅为函数调用保留,还为这些函数内部的变量保留...

You are using a lot of automatic storage duration here. 您在这里使用了大量的自动存储期限 It's unlikely your compiler and architecture will permit any more than about 1Mb of such stuff. 您的编译器和体系结构不太可能允许此类内存超过1Mb。

Fortunately, the solution is trivial. 幸运的是,解决方案很简单。

Use std::vector in place of the arrays: eg 使用std::vector代替数组:例如

struct MAG {
    // int racksCount; no need for this if you use a std::vector
    std::vector<RACK> racks;
};

and so on. 等等。 std::vector puts most of its payload in dynamic memory which is why this approach will work. std::vector将其大部分有效负载放入动态内存中 ,这就是这种方法行之有效的原因。 There are other C++ standard library containers too, but a good rule of thumb is to use a std::vector unless you have a good reason not to. 也有其他C ++标准库容器,但是一个很好的经验法则是使用std::vector除非您有充分的理由不这样做。

Yes, you run out of stack. 是的,您用完了堆栈。

This is especially the case because you are copying data from place1 in shelf1 and the same for all oothers, all that just for one element. 尤其是这种情况,因为您要从shelf1 place1复制数据,并且所有其他对象都复制相同的数据,所有这些仅针对一个元素。 Use a std::vector instead of static arrays. 使用std::vector而不是静态数组。

If you can't because it's an assignment, you can still use new / delete to use heap instead of stack. 如果不能,因为它是分配,则仍然可以使用new / delete来使用堆而不是堆栈。

If not, cross your fingers, and try to use: 如果没有,请用手指交叉,然后尝试使用:

MAG mag1 = {1, {{3, {{2, {{1, {1,2}}}}}}}};

Ugly, and you don't know what objects you are populating, but should work on modern compilers. 丑陋的,您不知道要填充什么对象,但是应该可以在现代编译器上工作。

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

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