What troubles does gcc compiler optimization cause ???

Source: Internet
Author: User

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 !!!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.