Today I saw a very interesting program, as shown below:
int main(){ const int a = 1; int *b = (int*)&a; *b = 21; printf("%d, %d", a, *b); return 0;}
When I first saw this program, I thought it was 21 or 21, but I was wrong.
It was hard to understand for a moment, so I output their addresses again:
int main(){ const int a = 1; int *b = (int*)&a; *b = 21; printf("%d, %d", a, *b); printf("\n%p, %p", &a, &*b); return 0;}
Their addresses are the same. I am more puzzled here, so I tried to check the assembly code.
int main(){ const int a = 1; int *b = (int*)&a; *b = 21; printf("%d", a); return 0;}
The compilation code is as follows:
Here we get at&t's assembly code, which is different from intel:
1. The command format is: command name, meta-operand, destination operand
2. Add % before the Register
3. Add $ before the operand
4, 0x4 (% esp) is memory addressing, which actually represents the content in the esp register + 4 (if you do not understand it very well, I hope to find the information by myself, I have limited knowledge)
First, let's look at the line labeled 1. The corresponding c statement is const int a = 1. This is where 1 is placed in the address 0x18 (% esp, next, let's look at the printf statement corresponding to number 2 and find that there is no reference value for the position where the address is 0x18 (% esp, instead, 1 is directly put to 0x4 (% esp) and then output.
Therefore, I personally think that the first result is caused by some optimizations made by the compiler. To prove my point, I modified the program:
int main(){ int c = 1; const int a = c; int *b = (int*)&a; *b = 21; printf("%d, %d", a, *b); return 0;}
Output result:
The corresponding assembly code is:
At Mark 1, we can determine where a is stored in 0x14 (% esp). At Mark 2, the corresponding printf statement. This statement processes parameters from right to left, 2. Processing is * B, 3 processing is a. At this time, we can see that we use an address instead of a numerical value. At the same time, we can see that the number is 0. we assign a value of 1 to c, when a is assigned a value, the compiler uses a value without a reference address.
Therefore, I guess the compiler has an optimization function in this regard: If a variable is assigned a constant during definition, when it is referenced, the compiler will directly use this constant value instead of the address reference to save time, but it also brings us extra trouble.
These are my personal opinions. I hope you can give me some advice !!!