Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allocating large buffers on the stack

Tags:

c++

c

linux

memory

Is it just a "bad manner" to allocate large buffers in a stack (if the stack has enough capacity)? I mean allocating 300-500 KB objects with an 8 MB stack in Linux systems. Or it may cause any errors? Also, are there any guidelines on how to use stack memory?

like image 739
dmitry Avatar asked Oct 16 '25 20:10

dmitry


2 Answers

There's no hard rule regarding the size of stack-allocated variables.

As a general rule, I prefer to not have any stack allocations larger than about 10KB. That way running out of stack space will be much less likely to be an issue. Anything bigger than that should be dynamically allocated, then freed when no longer needed.

like image 115
dbush Avatar answered Oct 18 '25 10:10

dbush


Yes, it's a bad manner. Objects of that size is to be dynamic allocated (or be static). No exceptions.

What if your program one day is to be used on a system with less default stack size? For instance windows.. then you "eat" half the stack in a single call.

like image 20
Support Ukraine Avatar answered Oct 18 '25 11:10

Support Ukraine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!