Ashwini Chaudhary Ashwini Chaudhary - 1 year ago 40
C Question

Is the order of memory addresses of successively declared variables always descending?

why does the hexadecimal value of pointer address returned is always in decreasing order?
for example here

int a
was declared before
int d
, so it's address always comes out to be greater than
, and same for
, I want to know that is this a fixed behavior or is this compiler dependent?
I am using
gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-1


int main(void){
int a=1;
int d=1;
char b='a' ;
char e='a';
float c=1.0;
float f=1.0;
if (&a>&d)
if (&a>&d && &b>&e && &c>&f)
printf("addresses are in descending order");

return 0;



a=0xbfc6bd98 //a>d
b=0xbfc6bd9f //b>e
c=0xbfc6bd90 //c>f
addresses are in descending order

PS: I am new to c

Answer Source

Found this nicely explained in Smashing The Stack For Fun And Profit, by Aleph One. Extracted the most relevant parts.

                         /------------------\  lower
                         |                  |  memory
                         |       Text       |  addresses
                         |                  |
                         |   (Initialized)  |
                         |        Data      |
                         |  (Uninitialized) |
                         |                  |
                         |       Stack      |  higher
                         |                  |  memory
                         \------------------/  addresses

                     Fig. 1 Process Memory Regions


   The stack consists of logical stack frames that are pushed when calling a
function and popped when returning.  A stack frame contains the parameters to 
a function, its local variables, and the data necessary to recover the 
previous stack frame, including the value of the instruction pointer at the 
time of the function call.

   Depending on the implementation the stack will either grow down (towards
lower memory addresses), or up.  In our examples we'll use a stack that grows
down.  This is the way the stack grows on many computers including the Intel, 
Motorola, SPARC and MIPS processors. 


   Let us see what the stack looks like in a simple example:

void function(int a, int b, int c) {
   char buffer1[5];
   char buffer2[10];

void main() {


With that in mind our stack looks like this when function() is called (each
space represents a byte):

bottom of                                                            top of
memory                                                               memory
           buffer2       buffer1   sfp   ret   a     b     c
<------   [            ][        ][    ][    ][    ][    ][    ]

top of                                                            bottom of
stack                                                                 stack

As you can see, new (local) variables are pushed on top of the stack. Depending on the design of the architecture the stack grows towards higher memory addresses or towards lower memory addresses, the latter in your case.

From the viewpoint of the C language specification the order of memory locations of subsequently allocated variables is unspecified. Therefore, it depends ...