Thread: address of stack variables

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    180

    address of stack variables

    Stack grows from high memory to low memory. Knowing that i declared 3 variables and was expecting that the first declared would have the biggest address, and the last one the lowest. It didn't turn out that way. Why is this?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define BUFFSIZE 8
    
    int main(int argc, char** argv)
    {
        int val = 7;
        char buff1[BUFFSIZE];
        char buff2[BUFFSIZE];
    
        printf("val is at %p\n", &val);
        printf("buff1 is at %p\n", buff1);
        printf("buff2 is at %p\n", buff2);
        return 0;
    }
    Code:
    OUTPUT:
    
    val is at 0x7ffcae0c5bbc
    buff1 is at 0x7ffcae0c5bc0
    buff2 is at 0x7ffcae0c5bd0

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by telmo_d View Post
    Stack grows from high memory to low memory.
    I guess Stack behaviour is implementation dependent; not to do with the CPU but the dependencies of your program e.g the C library implementation, the linker...

  3. #3
    Registered User
    Join Date
    Apr 2015
    Posts
    180
    I thought i had figured out it works on my system that is just allocated space and then put variable starting at the end of the allocated space in the order they appeared. Turns out that's not correct too in another little progrma i had an integer and a char array. Integer variable always had smaller adress than buffer regardless of where it was declared, before or after buffer.

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Although the stack on your machine probably grows downwards that doesn't mean that your compiler is constrained to push your variables to the stack in the order you've listed them. There's nothing in the standard (or any reason you should care) about the order the appear on the stack.

    They're not usually "pushed" to the stack anyway. Usually the generated code simply subtracts the necessary amount from the stack pointer and uses the appropriate addresses for your variables. There's nothing to "push" for buff1 and buff2, anyway. They just need space to exist and initially contain whatever was in that stack memory previously.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    You could determine the direction of stack growth with nested functions.

    Declare var1 inside function1(). Before returning from function1(), pass the address of var1 to function2().

    Declare var2 in function2(), and examine addresses of var1 and var2.

    -

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Figuring out if address is in heap or stack
    By Rubik in forum C Programming
    Replies: 10
    Last Post: 09-18-2013, 07:35 PM
  2. Accessing variables with memory address
    By ITAmember in forum C Programming
    Replies: 54
    Last Post: 06-28-2009, 03:35 AM
  3. local stack variables
    By Amyaayaa in forum C++ Programming
    Replies: 25
    Last Post: 10-07-2008, 01:13 PM
  4. Address of variables?
    By alex1067 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 10:47 PM
  5. 2 Variables, 1 Address?
    By Epo in forum C++ Programming
    Replies: 7
    Last Post: 01-02-2004, 09:05 PM