C & C++: What kinds of static memory allocation consume stack space?

  allocation, c++, memory, stack

This is a question about static memory in C and C++ environments.

Put simply I am interested to know what kinds of (static) memory allocation consume stack space.

There are two things that initially come to mind:

double x;

If I understand correctly, this does not consume stack space. It will be compiled to something like db 8 or rb 8(?) in assembly language and will live in the data segment of loaded executable code, before the actual executable code containing functions, etc.

double x[10];

If I understand correctly this does consume stack space. The generated assembly code will be something like sub sp 80, to reserve 80 bytes of space on the stack.

Finally, there may be other more complex situations for example the equivalents using user-defined types. (Which may also be polymorphic, in the case of C++.)

class myclass;
myclass y[100];

This (presumably) behaves the same as the above case of double x[10] – but maybe not.

The reason for asking this is I am considering writing some code and trying to completely eliminate dynamic memory from the application. I don’t know if this is likely to be a good or bad design choice as I don’t know with enough certainty which types will consume stack space and might result in a higher chance of a stack overflow due to the reduced available stack space. (Which is another assumption, I assume there is a limit to the amount of user data/variables which can be allocated on the stack, but please correct me if I am wrong about that. How do I know in advance what this limit will be?)

Source: Windows Questions C++

LEAVE A COMMENT