What is the idea of ​​using a stack for local variables?

In C, as many of you know, the stack contains all the local variables. The stack, which is the first in the last data structure, means that you can only access what was recently superimposed on it. Therefore, the following code is given:

int k = 5; int j = 3; short int i; if (k > j) i = 1; 

Obviously, this is useless code that doesn't make any real sense, but I'm trying to wrap something around.

For a short int declaration, I assume that 2 bytes will be allocated on the stack. For int k and int j, the values ​​5 and 3 are assigned for both 4 bytes. Thus, the stack looks like this:

 ---------- <- stack pointer int i ---------- int k = 5 ---------- int j = 3 ---------- 

so for the if statement, you will need to put int i to go to conditions k and j, and if so, where does this happen? It all seems very time consuming and tedious if it is so that C executes local variables.

So is it really how C is it or am I doing all this?

+5
source share
4 answers

Have you ever waved it so slightly.

Yes, local ( auto ) variables are usually stored on the stack. However, when reading, they are not popped out of the stack; they are referenced by the offset from the stack pointer.

Take the following code:

 x = y + z; 

where each of x , y and z is allocated on the stack. When the compiler generates equivalent machine code, it will refer to each variable to the offset from the given register, for example:

 mov -8(%ebp), %eax add -12(%ebp), %eax mov %eax, -4(%ebp) 

In x86 architecture, %ebp is the frame pointer; the stack is divided into frames, where each frame contains the parameters of the function (if any), the return address (that is, the address of the command following the function call), and local variables (if any). On systems that I'm familiar with, the stack grows “down” to 0, and local variables are stored “below” the frame pointer (lower addresses), hence a negative offset. The code above assumes that x is at -4(%ebp) , y is at -8(%ebp) , and z is at -12(%ebp) .

When the function returns, but not earlier, everything will be removed from stack 1 .

EDIT

Note that none of this is provided by the C language definition. The language does not require the use of a runtime stack at all (although the compiler will be a bitch to implement without it). It simply defines the lifetime of auto variables as from the end of their declaration to the end of their scope. The stack makes this easy, but it is not required.


1. Well, the stack and frame pointers will be set to new values; the data will remain where it was, but that memory is now available for something else.
+4
source

The stack is not a stack. This is still random memory, which means you can access any location at all times. The sole purpose of stack discipline is to give each function a call to its own, private area of ​​memory, which the function can be sure is not used by anyone else.

+7
source

The call stack is a stack, but it is not used as you imagine. Each time a call is made from a function, the return address (program counter) is pushed onto the stack along with local variables. When each function returns, the stack is called by what is called a "stack frame", which includes variables. Inside each function, memory is treated as random access. The compiler that generated the code that ordered the local variables on the stack knows exactly how far they are from the stack frame pointer, so it does not need to click and call the individual local variables.

+4
source

At the top of the stack, on the Intel processor and many others, the address stored in the cpu register is referenced, calls its SP, which is copied to the base pointer, calls its BP; Many machine instructions allow you to call an address expression consisting of the current BP in combination with a byte offset. so in your example i will be offset 0, j will be offset -2, and k will be offset -6.

if the contents of the addresses -6 (BP) and -4 (BP) were just resolved. actual offset values ​​may differ from implementation to implementation; but what a general idea ...

0
source

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


All Articles