Const and volatile in C language and constvolatile

Source: Internet
Author: User

Const and volatile in C language and constvolatile

Baidu was abused by volatile during the interview, which is also used in many places in the kernel. The probability of this interview is too high, so some results have been collected for your reference, most of them come from Baidu, which is quite clear and will be updated later when I read the code.

Baidu knows that the following questions are raised:

#include "stdio.h"int main(void){    const char i = 1;    char * j = (char *)&i;    printf("%d,%d,%p,%p\n",i,*j,&i,j);//1,1    *j = 2;    printf("%d,%d,%p,%p\n",i,*j,&i,j);//1,2    *j = 3;    printf("%d,%d,%p,%p\n",i,*j,&i,j);//1,3}

Why is I not changed? You may also find that their addresses are the same.

Drunk, I also changed with j when I debug, but I changed to 1 when I output. I hope you can discuss this issue from the compilation perspective.

I have made some changes to the answer, which is similar to the vc compilation result, so I will reference others' diagrams.

1. This is the assembly code with const modifier and without const modifier.

Variable I is stored in the eax register. The value of the const expression register cannot be modified.

* J = 2 when the number of rows is 22nd; if the value is assigned, edx is operated if the value is const modified.

Without const, you can directly perform operations on eax.

In the compiler debugging mode, the I value is changed to 2 because the compiler sees edx and the actual output is 1. For more information, see add const to row 16th, optimized the I corresponding to the first % d in the printf function and directly passed 1. If there is no const, It is the value obtained by the passed I address, in the end, it is still a compilation optimization problem. I found your problem here in the kernel. adding volatile can indeed force the compiler to pass the I address to the value, the value of the Register is displayed in the debugger, so it is changed.

2. logically, the const modifier means that the variable value cannot be changed. as for how to implement the background, you can look at the Assembly. A simple value assignment expression is also implemented by three assembly languages.

3. const means that the limitation cannot be changed, the limitation is the variable, the variable cannot be modified, the limitation is the pointer, And the pointer value cannot be modified.

The method is defined in the class. The value of the member variable cannot be changed in the method. The returned value cannot be modified.

 

Volatile is designed to modify variables accessed and modified by different threads. If you do not add volatile, it will basically lead to the following results: either you cannot write multi-threaded programs, or the compiler loses the chance of a lot of optimization.

A variable defined as volatile means that this variable may be unexpectedly changed, so that the compiler will not assume the value of this variable. Precisely, the optimizer must carefully re-read the value of this variable every time when using this variable, rather than using the backup stored in the register. The following are examples of volatile variables:

1). Hardware registers of parallel devices (for example, Status Registers)

2). Non-automatic variables that will be accessed in an interrupt service subroutine)

3) variables shared by several tasks in multi-threaded applications

This is the most basic problem between C programmers and embedded system programmers: Embedded System programmers often deal with hardware, interruptions, RTOS, and so on. All these require volatile variables. If you do not know volatile content, it will lead to disasters.

If the subject correctly answers this question (well, I suspect it will be the case), I will go a little deeper to see if this guy really understands the full importance of volatile.

1) can a parameter be const or volatile? Explain why.

2) can a pointer be volatile? Explain why.

3). Can the following function be used to calculate the square of an integer achieve the expected design goal? If not, try to answer the following questions:

 

1

2

3

4

Int square (volatile int * ptr)

{

Return (* ptr) * (* ptr ));

}

The answer is as follows:

1). Yes. One example is read-only status registers. It is volatile because it may be unexpectedly changed. It is const because the program should not try to modify it.

2). Yes. Although this is not very common. One example is when an interrupt service subroutine modifies a pointer to a buffer.

3). This code is a prank. The purpose of this Code is to return the pointer * ptr points to the square of the value. However, since * ptr points to a volatile parameter, the compiler will generate code similar to the following:

 

1

2

3

4

5

6

7

Int square (volatile int * & ptr) // here the parameter should be declared as a reference. Otherwise, only the copy will be used in the function body, and the external cannot be changed.

{

Int a, B;

A = * ptr;

B = * ptr;

Return a * B;

}

* The value of * ptr may change between two value statements, so a and B may be different. As a result, this Code may not return the expected square value! The correct code is as follows:

 

1

2

3

4

5

6

Long square (volatile int * ptr)

{

Int;

A = * ptr;

Return a *;

}

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.