Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22017

How are local variables stored on the call stack (GCC compiler)

$
0
0

I apologize if this has been asked before, I did try looking at old questions but I am still confused as to how the GCC compiler stores variables on the stack. I understand that it grows downward but what about local variables specifically? Do they just get allocated a block of memory or are they stored in a particular order, I assume it's a particular order but I get confused when I try to visualize it with simple programs:

int main(int argc, char* argv[]){  char *name = "abc";  char *p = "123";  char *q = "hello";  printf("%p\n%p\n%p\n", &q, &p, &name);}

For example this program has three character pointers and I am trying to print out their addresses in a way that would mimic the call stack. When I print them in the order they are declared in the variable with the highest address is the first one printed:

0x7fff5b9e29480x7fff5b9e29400x7fff5b9e293M

Making me think that the local variables would get pushed to the stack from last declared to the most recently declared. I guess what I am asking is if this is an accurate visualization of the stack with the order of the elements being added to the stack "q" then "p" then "name"? Or would I print them in opposite order if I wanted to print out something that mimics the order of the stack. Not a homework question or anything just trying to better understand how the stack works before I get into memory allocation.


Viewing all articles
Browse latest Browse all 22017

Trending Articles