Where the value of the variables is stored in C

In the following code segment:

int func() { int a=7; return a; } 

Is a code segment where the value 7 is stored in an executable file? Or is it in the data segment or in the code segment? Will the answer depend on the operating system or compiler?

+1
source share
3 answers

Each executable format has several sections. One of them is text , contains the binary code of the assembly. One of them is heap , where malloc -ed data is found, and on stack , where local variables are stored. There are several others, but right now it doesn’t matter. The above three are common.

Now local data, such as your a , is on the stack. In the executable file, the value is stored in the text section.

I added the main code to your code (return 0) compiled with -g , then executed objdump -CDgS a.out and searched for 0x424242 (I replaced your 7 with a less likely chance of appearing in the code).

 00000000004004ec <func>: int func() { 4004ec: 55 push %rbp 4004ed: 48 89 e5 mov %rsp,%rbp int a=0x42424242; 4004f0: c7 45 fc 42 42 42 42 movl $0x42424242,-0x4(%rbp) return a; 4004f7: 8b 45 fc mov -0x4(%rbp),%eax } 4004fa: 5d pop %rbp 4004fb: c3 retq 

As you can see, c7 45 fc 42 42 42 42 means that the value is stored in the generated file. In fact, this is the case when looking at the binary via xxd :

 $ xxd a.out | grep 4242 00004f0: c745 fc42 4242 428b 45fc 5dc3 5548 89e5 .E.BBBB.E.].UH.. 

You can recognize the above conveyor line in the xxd fragment.

+3
source

Since a implicitly auto (i.e. not extern or static ), it is stored on the call stack .

In fact, the compiler can optimize this: perhaps, in your case, during optimization it will remain in the register (or it will constantly multiply and constantly add up): there is no need to allocate a call stack slot for your a

This, of course, is a compiler, a target platform, and an operating system dependent one. For GCC, the compiler understands the internal representation of Gimple (via -fdump-tree-all or using the MELT probe ) and look at the generated assembler code (use -fverbose-asm -S -O )

See also this answer , which gives a lot of links.


GCC 4.8 on Linux / x86-64 compiles (with gcc -S -fverbose-asm -O ) your function into:

  .globl func .type func, @function func: .LFB0: .cfi_startproc movl $7, %eax #, ret .cfi_endproc .LFE0: .size func, .-func 

So, you see that in your particular case the extra space is not used for 7 , it is directly stored in %eax , which is a register (defined in ABI conventions) to hold the returned result.

The value 7 stored in machine code inside the movl machine instruction. When func is executed, then 7 is loaded into the %eax register containing the return result of func .

+2
source

Depending on the example code, the variable "a" enters the call stack, places for storing local variables along with information about the function call, such as program counter, return addr, etc.

0
source

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


All Articles