compiler optimization of C language

Source: Internet
Author: User
Tags volatile

The C compiler will optimize variables and code, and let's take a look at the following example.

int a,b,c;

A=1;

B=a;

C=b;

This program normally works like this: First put a point in the memory space into 1

And then read the number in the memory space of a pointing to the memory space where B points,

Finally, the number in the memory space where B points is read out and placed in the memory space C points to.


However, the compiler will be a certain optimization of this program, compile time, directly put 1 into three memory space.

This is a good thing in normal operation, but there will always be some special situation, for example, an interrupt program suddenly changed the value of a, then this happens: B and C should be a change after the value, but the compiler after the optimization has become a a,b,c is 1, which makes people depressed.


The workaround, then, is to add a volatile keyword to the definition of the variable, such as:

volatile int A;

This way the compiler will not be able to give you any optimizations when it encounters an operation of a, which ensures that your program works as you expect, but that if you use volatile in large quantities, without optimization, you can reduce the efficiency of your program.

Originally C language running efficiency is much lower than the assembly, so that volatile in addition to try to consider as well as possible, because many programs run in particular focus on efficiency.


The second optimization is the Restrict keyword, which is used only to modify pointers, and it is not useful to modify different variables.

When you use a restrict-modified pointer that is not changed by something else, to improve the efficiency of execution, you will be modified at compile time.

For example, the following function:

int main (void)

{

int *restrict P1, *restrict P2, A;

*p1=3;

*p2=5;

a=2+ (*P2);

return 0;

}

After this, we did the optimization, the compiler at the time of compilation will directly convert the *P2 to 5 (because there is no instruction to modify it, so the direct use of 5来 instead of *P2)

There are pros and cons to optimization, so use it carefully anyway.


compiler optimization of C language

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.