Memory addresses of local and global variables

1

as far as I knew when a program is loaded into memory uses 4 segments: text, data, stack, and heap. The global variables are recorded in the data segment while the local variables in the stack. In addition, by theory it is known that the addresses of the data segment must be lower than those of the stack; but I do not see that when executing this little program

#include <stdio.h>

static int a = 10;

void main( void ) {
  int b = 5;

  printf( "%p %p", &a, &b );
}

What I see as output is the global variable being stored in 0x100402010 while b in 0xffffcc0c . But obviously 0x100402010 is greater than 0xffffcc0c , which seems to contradict, according to the theory, the way in which the memory assigned to a program is organized. Why is the address of b (which is on the stack) smaller? Or does not% p not allow me to see the full memory address and only let me see the last 4 bytes? EYE: I'm using C, gcc, and Win-64 bits.

    
asked by Fredy Cuenca 13.09.2017 в 21:21
source

1 answer

2
  

Why is the address of b (which is on the stack) smaller?

The language standard does not determine where each variable should be stored. That is something that remains at the discretion of the tuple compiler-Operating System. If the Operating System imposes a certain organization of the memory the compiler has no choice but to apechugar with it.

In any case I do not see what advantages it can provide that one region of memory is before another ... or the other way around.

  

Or does% p not allow me to see the full memory address and only let me see the last 4 bytes?

A byte has a typical size of 8 bits ... in hexadecimal one byte occupies exactly one digit (0..F). The addresses you indicate are 64 bits and proof of this are the 8 digits they have (8 digits * 8 bits per digit = 64 bits)

    
answered by 14.09.2017 в 08:54