Are local initial values ​​stored in?

#include <stdio.h> int main() { int i = 10; return 0; } 

In the above program, where exactly is the value 10 stored?

I understand that the variable I am stored on the stack. the stack is populated at run time. From "where exactly" 10 goes.

+4
source share
7 answers

10 is a constant, so the compiler will use the number 10 directly in the executable part of your program as part of the CPU instructions.

Here is the assembly created on my system with gcc :

 movl $10, -4(%rbp) 

( 4 is because int is 4 bytes long)

Note that all these things are part of the implementation, but in practice this happens. The language itself does not indicate this data.

+5
source

10 is a "literal" that will be generated by the compiler at compile time. And then it will be assigned to your variable on the stack. how

 mov eax, 10; mov [0x12345678], eax; 

This is a pseudo code, but it will assign your variable i (the address here is 0x12345678) to the value 10 previously stored in eax .

+4
source

The "stack" is the memory area allocated by the operating system for your program, separate from the "heap" and global variables and executable code.

When a function is called, there is code to push the arguments onto the stack, and then space is reserved for local variables. When a function returns this space and all arguments are pushed out of the stack, so memory can be reused by the next function.

This is a very simple and crude description, and many details differ between systems and compilers.

+2
source

There will be an explicit machine code instruction that sets i .

Sort of:

  MOV AL, 10 
+1
source

After compiling and linking your executable file contains several segments. Two types of these segments:

  • text segments - containing the actual code
  • data segments - containing static data.

(there are other types)

The value 10 is stored either in the text segment (as an instruction for setting 10 to a specific address or register), or stored as data in a data segment (which is extracted by code and stored at a specific address / register).

The compiler decides which is better (most effective for given compilation flags). But I believe that it is “stored” in the text segment, since the value 10 is pretty simple to “create in code” (as some other answers show).

More complex data (structures, rows, etc.) is usually stored in the data segment.

+1
source

The value 10 is stored in a physical source file that contains the source code. At run time, this value is passed to a variable called i , which has an automatic storage duration and is of type int . That is all that matters. Any further questions are not productive in the field of general-purpose programming languages ​​such as C.

Notice how most answers mention compilers? What if the interpreter is used to translate the C source code directly into behavior ? Are these answers saved?

+1
source

I prefer not to "give a man a fish" if you ... You can check it yourself:

Take your code and create an object file from it:

 > gcc -c file.c -o file.o 

Then dump the object in the generated file:

 > objdump -d file.o Disassembly of section .text: 0000000000000000 <main>: 0: 55 push %rbp 1: 48 89 e5 mov %rsp,%rbp 4: 48 83 ec 10 sub $0x10,%rsp 8: c7 45 fc 0f 00 00 00 movl $0xa,-0x4(%rbp) // Right here you can see // the raw value 0xa (10) // being set, so it in the // .text section 
+1
source

All Articles