Are variables randomly allocated in memory?

Asked

Viewed 132 times

6

In a book about C, in which I started studying variables, it said that variables were randomly put into 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 (i.e., within a function), they are in the stack, i.e., sequentially. And the global variables are randomly, i.e., in the heap. If I put one printf() in 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 to understand this?

  • Remember that not all architectures/platforms have the same type of memory management. A good example would be virtual memory management, which not all systems/ platforms have one! When the author says that variables are randomly placed in memory, means that who controls these addresses is the system/platform on which the code is being executed. These memory addresses are returned by the system and cannot be determined in a "portable". By the way, what is the publication date of the book you mentioned ?

  • Book of 2007, called Linguagem C, by Luis Damas.

1 answer

3


Randomly is never the term, always has a certain determinism, just no way to anticipate where exactly when writing code, only at runtime.

And I’m going to talk here about virtual memory, physical memory is certainly organized differently and things will not even be sequential physically, but it is not random, and this does not affect your code. Let’s just get down to business.

First make sure you understand about stack and heap.

The C language does not determine that it is so, I will talk about how it actually works.

Do not use global variables. They are in a static area, which is not the heap, is reserved by the compiler in advance.

Stack It’s a stack, so it’s something sequential by definition. You have no control over where exactly she will be (you can see this at runtime, as you did in the question code), but it is far from random.

In the heap It’s also not random, it has criteria, but it’s a little more messy. It’s a lot, so the organization leaves to be desired, the goal is another. But it is also not spread all over memory without a sense. And some variables will be in sequence yes, especially in arrays. The organization just might not be so obvious.

The book may have made a simplification. Or you may have misinterpreted what was written. Or the book is really bad.

I made simplifications, it’s much more complicated than that, but so it’s understandable.

  • I think it was for simplicity, because the book is great. In the second chapter already enters the concept of variables, so there was no way to enter this concept of memory stack and heap.

  • One more question: the functions are also placed in the memory stack?

  • No, stay in the static area.

  • I understand, but according to this site: https://blog.pantuza.com/artigos/heap-vs-stack functions are on the stack

  • Nowhere in this text is it written that the functions stay on the stack.

  • Ah Yes, I was able to clarify my doubt, thank you very much.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.