简体   繁体   中英

Is it a good idea to extend the size of the stack for large local data processing?

My environment is gcc, C++, Linux. When my application does some data calculation, it may need a "large" (may be a few MBs) number of memory to store data, calculation results and other things. I got some code using kind of new , delete to finish this. Since there is no ownership outside some function scope, i think all these memory can be allocated in stack.

The problem is, the default stack size(8192Kb in my system) may not be enough. I may need to change stack size for these stack allocation. Morever, if the calculation needs more data in future, i may need to extend stack size again.

So is it an option to extend stack size? Since it cannot be allocated for specific functions, how will it impact on the whole app? Is it REALLY an improvement to allocate data on stack instead of on heap?

You bring up a controversial question that does not have a direct answer. There are pros and cons on each side. In particular:

  1. Memory on the heap is more easy to control: you can check the return value or allow throwing exceptions. When the stack overflows, your thread is simply unloaded with a good change that debugger will not show anything meaningful.

  2. On the contrary, stack allocations happen automatically, and you do not need to do anything specific. I always favor this simplicity.

There is nothing fundamentally wrong in allocating large amounts of data on the stack. At the end of the day any type of memory is finally a memory. This is means that the total amount of required memory is what really matters. Where this memory is allocated is less important. When there is enough memory for your application to work, there is no difference where the memory is allocated. It can be static for example.

Different systems have different rules of allocation. This means that final decision may depend on the actual system.

While it's true that stack allocations are more efficient (more apparent in multi-threaded programs), but if the usage pattern in your case is "allocate a big chunk of memory, process the data, deallocate it", then there won't be much of improvement.

Instead rewrite the code to use RAII, eg std::vector or std::unique_ptr so there won't be explicit buggy delete s.

If you use Linux, you can change the stack size with the ulimit command. However, I think the memory which allocated from the heap also is good for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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