Home > Mobile >  Allocating large buffers on the stack
Allocating large buffers on the stack

Time:11-20

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?

CodePudding user response:

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.

CodePudding user response:

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.

  • Related