Why are C variables stored in specific memory locations?

Yesterday I had an interview where the interviewer asked me about storage classes, where variables are stored.

My war with the answer:

Local Variables are stored in Stack. Register variables are stored in Register Global & static variables are stored in data segment. The memory created dynamically are stored in Heap. 

The next question he asked me was: why are they stored in this particular memory area? Why is the Local variable not stored in register (although I need the auto variable, which is very often used in my program)? Or why are global or static variables not stored on stack ?

Then I was clueless. Please help me.

+6
source share
5 answers

The C language does not determine where any variables are stored. However, it defines three storage classes: static, automatic, and dynamic.

Static variables are created during program initialization (before main() ) and remain in effect until the program terminates. A scope object ("global") and static variables are categorized. Although they are usually stored in a data segment, the C standard does not require this to be the case, and in some cases (for example, C interpreters) they can be stored in other places, such as a heap.

Automatic variables are local variables declared in the function body. They are created when the program flow reaches its announcement and is destroyed when they go out of scope; new instances of these variables are created for recursive function calls. The stack is a convenient way to implement these variables, but again, this is not required. You could also implement automation on the heap if you selected, and they are usually placed in registers. In many cases, an automatic variable will move between the stack and the heap during its lifetime.

Note that the register annotation for automatic variables is a hint - the compiler does not have to do anything with it, and indeed many modern compilers completely ignore it.

Finally, dynamic objects (in C there is no such thing as a dynamic variable) refer to values ​​created explicitly using malloc , calloc or other similar distribution functions. They arise upon explicit creation and are destroyed upon explicit liberation. A heap is a convenient place to place them, or rather, defines a heap based on the ability to make this distribution style. But then again, a compiler implementation can do whatever it wants. If the compiler can perform static analysis to determine the lifetime of a dynamic object, it can transfer it to a segment or data stack (however, some C compilers do this “escape analysis”).

The key conclusion here is that the C language standard only defines the lifetime of a given value. And the minimum boundary for this lifetime is that it can remain longer than required. Exactly how to place this in memory is a subject in which linguistic and library implementation gains significant freedom.

+12
source

Because the storage area determines the area and lifespan of the variables.

You select the storage specification depending on your requirement, i.e.:
Service Life:. The duration of waiting for a particular variable must be active and valid.
Area: The area (s) where you expect the variable to be available.

In short, each storage area provides different functionality, and you need different functionality, hence different storage areas.

+16
source

This is actually just a convenient implementation.

The compiler could, if it wanted, generate local variables on the heap if it wanted to.

It is easier to create them on the stack, because when you exit the function, you can set the frame pointer with simple addition / subtraction depending on the direction of the stack growth and, thus, automatically free up the used space for the next function. Creating local residents on the heap, however, would mean a lot of housework.

Another point is that local variables should not be created on the stack, they can be stored and used only in the register, if the compiler considers this more suitable and has enough registers for this.

+1
source

Local variables are stored in registers in most cases, because the registers are popped and unloaded from the stack when making function calls. It looks like they are on the stack.

Actually there are no variables like register variables, because it's just some kind of rarely used keyword in C that tells the compiler to try to put this in registers. I think most compilers simply ignore this keyword.

That is why you were asked more because he was not sure if you understand the topic. The fact is that register variables are actually on the stack.

0
source

in embedded systems we use different types of memory (read-only (unstable (ROM)), lossless read (EEPROM, PROM, SRAM, NVRAM, flash), volatile (RAM)), and we also have different requirements (cannot change, and also remain after turning on the power, may change and also remain after cycling the power, may change at any time) according to our data. we have different sections because we have to optimize our data requirements for the different types of memories available.

0
source

Source: https://habr.com/ru/post/916624/


All Articles