dmitry
dmitry

Reputation: 41

Allocating large buffers on the stack

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?

Upvotes: 4

Views: 1238

Answers (2)

4386427
4386427

Reputation: 44284

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.

Upvotes: 1

dbush
dbush

Reputation: 223992

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.

Upvotes: 1

Related Questions