1

I thought the memory address of variable become bigger and bigger, until I tried this code :

#include <stdio.h>  

int main()
{
    int IamfirstVariable = 9;
    char array1[10] = {'0','1','2','3','4','5','6','7','8','9'};
    char array2[10] = {'0','1','2','3','4','5','6','7','8','9'};
    char IamLastVariable = '0';
    printf("variable start :%p\n",&IamfirstVariable);
    printf("array1 address start :%p    end :   %p \n",&array1[0],&array1[9]);
    printf("array2 address start :%p    end :   %p \n",&array2[0],&array2[9]);
    printf("variable end :%p\n",&IamLastVariable);
    return 0;
}

Output:

variable start :0xbfb02c3c
array1 address start :0xbfb02c32    end :   0xbfb02c3b 
array2 address start :0xbfb02c28    end :   0xbfb02c31 
variable end :0xbfb02c27

I am stucked by this . It seems that the last declared variable get the smallest address!

Can anyone explain this for me ?

----------------------EDIT----------------------------------------

I read the links in answer and got anohter question:

Since the stack glow downaward , why the address of array still glow upward ?

Lidong Guo
  • 2,817
  • 2
  • 19
  • 31
  • If you are interested, try the heap instead of stack, and see the result. – Yu Hao Aug 31 '13 at 14:29
  • @YuHao I tryed , it give me discontinuous but grow upaward memory address. I can understand this, but I still can't understand the reason stack grow downaward ! Is it only to have a stack limit ? – Lidong Guo Aug 31 '13 at 14:40
  • Because on many platforms, stack grows downward, while heap grows upward. Try to search the image of stack and heap in memory. A picture is more than any words. – Yu Hao Aug 31 '13 at 14:46

3 Answers3

3

Because on Intel the stack grows downward (see for example https://stackoverflow.com/a/1691818/613130). Your array1 and array2 being local variables are allocated on the stack. Note that this is platform-specific, not C-specific. On different platforms it could grow upward.

If you are interested on which architectures have stacks that grow upward or downward, a similar question was already asked: https://stackoverflow.com/a/664779/613130

Community
  • 1
  • 1
xanatos
  • 109,618
  • 12
  • 197
  • 280
2

Because variables are stored in the stack.

Stack memory allocation use a very simple algorithm: the stack pointer points the first empty space and is moved down each time you add a variable.

See also: Understanding the Stack

Benoit Blanchon
  • 13,364
  • 4
  • 73
  • 81
1

lets see Empty stack

0x00000000   |       |
    :        |       |
    :        |       |
    :        |       |
    :        |       |
0xFFFFFFFF   |       | ==>position of stack pointer
            -----------    

when variable one declared ,Memory to the var1 will be allocated in stack and then stack pointer position comes to up.

0x00000000   |       |
    :        |       |
    :        |       |
    :        |       |
    :        |       |==>position of stack pointer
0xFFFFFFFF   | var1  |
            -----------

when variable two declared ,Memory to the var2 will be allocated in stack and then stack pointer position comes to up.

0x00000000   |       |
    :        |       |
    :        |       |
    :        |       |==>position of stack pointer
    :        | var2  |
0xFFFFFFFF   | var1  |
            -----------

so the last declared variable get the least address and first declared variables got max address.

Gangadhar
  • 10,248
  • 3
  • 31
  • 50