C ++ Summary of precautions related to "class" (2): volatile and mutable

Source: Internet
Author: User

I. Volatile usage

You can declare a member function as volatile. If the value of a class object may be modified in a way that the compiler cannot control or detect, for example: if it is a data structure that represents the I/O port, it is declared as volatile similar to the const Class Object. For a volatile class object, only volatile member functions, constructors, and destructor can be called:

Class Screen {
Public:
Char poll () volatile;
 
//...

};
Char screen: poll () Volatile {...}

 

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) An Interrupted service subaccountProgramNon-automatic variables that will be accessed in)
3) variables shared by several tasks in multi-threaded applications

People who cannot answer this question will not be hired. I think this is the most basic problem to distinguish between C programmers and embedded system programmers. Embedded people often deal with hardware, interruptions, RTOS, etc. All of 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 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); what are the following function errors:

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 repairs a pointer to a buffer.
3) this sectionCodeA little abnormal. 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:

Int square (volatile int * PTR)
{
Int A, B;
A = * PTR;
B = * PTR;
Return a * B;
}

* The value of * PTR may be unexpectedly changed, 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:

Long square (volatile int * PTR)
{
Int;
A = * PTR;
Return a *;
}

 

Volatile is equivalent to telling the compiler that the variables declared by the compiler are changeable, uncertain, and may be changed by external programs (such as interrupt programs). The compiler is prohibited from optimizing its read/write operations, if:
Int I;
Then the compiler may optimize it and place it in the CPU register, which is good in most cases. However, in some cases, we will require some variables to be in the memory (such as drivers, interrupt handlers, and so on). In this case, the compiler optimization is a problem. To avoid this situation, we should define it as follows:
Volatile int I;

PS: Volatile is usually used to prevent Compiler Optimization operations. For example, you have an inaccurate latency function:

Void delay (unsigned int timeout)
{
Unsigned int I;
For (I = 0; I <timeout; I ++ );
}

 

Some compilation will be smart enough to notice that this function does nothing in essence and will optimize the call to this function. However, this is wrong, so you should declare it as follows:

 

Volatile void delay (...)
{
// Same as above
}

 

Ii. mutable data member

Some problems occur when we declare a screen object as Const. Our expected behavior is that once the const Screen Object is initialized, its content cannot be modified, however, we should be able to monitor the content of the Screen Object. For example, the following Screen Object CS is provided:

Const screen CS (5, 5 );

We want to monitor the content in location (3, 4). We do this:

// Content of the read position (3, 4)
// Oh! Not working
CS. Move (3, 4 );
Char CH = cs. Get ();

However, this cannot work. Do you know why? Move () is not a const member function, and cannot be easily converted into a const member function. The definition of move () is as follows:

Inline void screen: Move (int r, int C)
{
If (checkrange (R, c ))
{
Int ROW = (r-1) * _ width;
_ Cursor = row + C-1; // modify _ cursor
}
}

We noticed that move () modifies the data member _ cursor. Therefore, it cannot be declared as const without modification. However, it may seem strange that _ cursor cannot be modified for a const object in the screen class. Because _ curso is only an index, modifying _ cursor does not modify the content of screen, we just want to remember the position of the screen to be monitored. Even if the Screen Object is const, _ cursor should be allowed to be modified, because this is required to monitor the content of the Screen Object, and does not modify the content of the screen itself.

To allow modification of a data member of a class, even if it is a data member of a const object, we can declare the data member as mutable (Changeable ), mutable data members are never const members. Even if they are data members of a const object, mutable members can always be updated, even in a const member function. To declare a member as a mutable data member, we must put the keyword mutable before the data member declaration in the class member table:

 

Class Screen {
Public:
// Member functions
PRIVATE:
String _ screen;
Mutable string: size_type _ cursor; // mutable Member
Short _ height;
Short _ width;
};

Now any const member function can modify _ cursor. We can declare the member function move () as const, even if moving () modifies the data member _ cursor, there will be no compilation errors:

// Move () is a const member function
Inline void screen: Move (int r, int c) const
{
//...
// OK: the const member function can modify mutable members.
_ Cursor = row + C-1;
//...
}

Now we can monitor the CS object of the screen object by performing the operation given at the beginning without errors. We noticed that only _ cursor is declared as a mutable data member, while _ screen _ height and _ width do not, because the value of these data members should not be changed in the screen Class Object of Const.

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.