My question is : Are the name of the variable and the data itself both stored in the stack?
I would like to know how the name of the variable is linked to the address memory in the stack (the data) and what does it.
Also how does anything know the number of bytes the type of the variable is composed of and how does it decides to read these exact number of bytes in the stack?
Does all the data stored in the stack occupy the same space, no matter the type of data it is?.
And the same questions with the heap?
Generally, I believe the following to be true in most practical implementations:
No, the name and actual data are not both stored on the stack.
The compiler keeps track of where the variable is on the stack, and when the compiler is done, all references to the variable (ie the name) has been substituted by a proper increse/decrease of the stack pointer to address the memory area where the data is stored.
No, they do not occupy the same space. A 4 byte var takes up 4 bytes. A 1000000 bytes variable take up 1000000 bytes (but that’s not recommended, usually).
The heap is a bit different… Maybe this page can answer your question a bit more: http://www.learncpp.com/cpp-tutorial/79-the-stack-and-the-heap