C-const and volatile

Source: Internet
Author: User

I. const and volatile analysis

1. const modifier variable

# It is read-only when the const variable is modified in C language. Its essence is variable.

# The const variable occupies space in the memory.

# Essentially, const is only useful to compilers and useless during runtime

The original const is not a real constant.


2. const modifier Array

# In C language, the const-modified array is read-only.

# The array space modified by const cannot be changed.

Const int A [5] = {1, 2, 3, 4, 5}

Int * p = (int *);

Int I = 0;

For (I = 0; I <5; I ++)

{

P [I] = 5-i; // Error

}

3. const modifier pointer

# Const int * p; // p variable. The content pointed to by p cannot be changed.

# Int const * p; // p variable. The content pointed to by p cannot be changed.

# Int * const p; // p is unchangeable, And the content pointed to by p is variable.

# Const int * const p; // p is immutable, and the content pointed to by p is immutable

Tip: left and right fingers

When const appears on the left side of *, the pointer points to a constant. When const appears on the right side of *, the pointer itself is a constant.

Const modifier function parameters and return values

# Const indicates that you do not want to change the parameter value in the function body.

# The Return Value of the const modifier function indicates that the return value cannot be changed and is mostly used to return pointers.

Const int * func ()

{

Static int count = 0;

Count ++

Return & count;

}

Volatile

1. volatile can be understood as "compiler warning indicator"

2. volatile is used to tell the compiler that the variable value must be retrieved from the memory each time.

3. volatile mainly modifies variables that may be accessed by multiple threads.

4. volatile can also modify variables that may be changed by unknown factors.


Int obj = 10;

Int a = 0;

Int B = 0;


A = obj;

Sleep (100 );

B = obj;

In the above Code, the compiler finds that obj is not used as the left value during compilation, so it will be "smart" to directly replace obj with 10, and assign a and B to 10.


After-school thinking

# Can const and volatile modify a variable at the same time?

# Const volatile int I = 0; in this case, what attributes does I have? If the compiler processes this variable?

A: Yes. In this case, I have the const and volatile attributes. I variables cannot be modified by program code during compilation, and the compiler cannot optimize compilation of I.

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.