Reputation: 543
Simply coding C file like bellow:
int main()
{
int a = 999;
return 0;
}
after compile and link using gcc, it will generate a executable file (e.g. .exe, .out)
But when I open (NOT RUNNING) the executable file with some Editor, I can not find the value of variable 'a' the number 999 which in hex is 0x3E7.
My question is:
P.S: I have a little knowledge about memory section like .data .bss .text .etc and assembly language. Evenly I can not find it using ollydbg.
Upvotes: 0
Views: 350
Reputation: 363882
One of the important functions of optimization is removing unused stuff. The behaviour of the program doesn't depend at all on the 999
, and a
isn't volatile
, so the assignment isn't part of any visible side-effect, and the program is exactly equivalent to int main(){return 0;}
It's a lot easier to look at compiler output in asm form.
On the Godbolt compiler explorer, you can set it up so you can see asm output for gcc -O0
and gcc -O1
at the same time, in different panes. https://godbolt.org/z/wNHEnN.
mov DWORD PTR [rbp-4], 999
is there at -O0
, along with stack-frame setup fluff. So the -O0
output will have 999
as a dword immediate operand to a mov
instruction. The variable is local, so you won't find a symbol-table entry for it in the .data
or .rdata
sections (like you would with a global with a static initializer).
See also How to remove "noise" from GCC/clang assembly output? for more about looking at compiler output.
Upvotes: 4
Reputation: 13134
There is no reason for the compiler to put the value 999
anywhere since it is not used anywhere. The program has the same observable behaviour regardless 999
being somewhere in memory or not.
ISO/IEC 9899:TC2 - 5.1.2.3 Program execution:
- In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced (including any caused by calling a function or accessing a volatile object).
Upvotes: 5