In a book about C, in which I started studying variables, I would say that the variables were randomly placed in memory, for example:
int a;
int b;
printf("a = %d\n", &a); --> 5000 (endereço) (%d ao invés de %p para simplificar)
printf("b = %d\n", &b); --> 7634 (endereço) (%d ao invés de %p para simplificar)
However, from what I've researched, the local variables (ie, within a function), they stay in the stack , that is, sequentially. And the global variables are randomized, that is, in heap . If I put a printf()
in the local variables, they are in sequential addresses, as in this case (compiled by GCC on Linux):
char a;
int n;
float b;
printf("a = %p\n", &a); --> a = 0x7ffeb85afd5f
printf("n = %p\n", &n); --> n = 0x7ffeb85afd60
printf("b = %p\n", &b); --> b = 0x7ffeb85afd64
So this contradicts what I learned in the book. How do you understand this?