Talk about Volatile and Const

xiaoxiao2021-03-06  43

User: 404082727 (walking as the end of the day)

============================================================================================================================================================================================================= Message group: group / alumni () ========================================= ========= Message Type: Chat record ======================================= ==============

2004-12-28 14:50:43 Lalala () const only indicates that the rear variable is initialized and cannot be assigned. This is just the limitations of compilation moments. That is, A is basically a normal variable, just COMPILER assigns it to you.

At runtime, A is different from other variables.

If int A = i; legal, const I = i; of course legal. Because such initialization is done at runtime.

The value of Const can even change! ! !

#include

INT Main (int Argc, char * argv []) {volatile const Int a = 0;

INT * P = (int *) & a;

* p = 1;

Printf ("* p =% D, A =% D, P =% P, & A =% P, & P =% P / N", * P, A, P, & A, & P);

Run results: (Cygwin, GCC)

* p = 1, a = 1, p = 0x22Feec, & a = 0x22Feec, & P = 0x22Fee8

Note: A and P are adjacent, all in the stack. If there is no volatile, the result is: * p = 1, a = 0, P = 0x22Feec, & a = 0x22Feec, & P = 0x22Fee8

This is completely the result of the compiler's own optimization

2004-12-28 14:53:17. Theash () thick, const just declares during the compilation period, not used to make him read only from the hardware. So there are many ways to deceive the compiler.

2004-12-28 14:53:45 Once the future () http://blog.9cbs.net/kendiv/archive/2004/128/231710.aspx gcc Chinese manual 2004-12-28 14:54:20 The key is the volatile, I haven't used it before.

2004-12-28 14:56:07 iCoding () is the opposite of Volatile and Const, equal to did not add const? [?]

2004-12-28 14:56:45 lalaala () Volatile said that this variable may be modified, the compiler does not replace it with constants

2004-12-28 14:57:30 icoding () volatile said that this variable may be modified --------------- Say and not say, the value of the variable will be modified.

2004-12-28 14:58:03 icoding () http://www.coreSoft.com.ru/ibbs/topic.asp?bbsid=2&topicid=25

2004-12-28 14:58:06 Walking up the end of the world () he did not have a relationship, Volatile told the compiler not to optimize the variable

2004-12-28 14:58:46 lalaala () constant

2004-12-28 14:59:14 Walking the endless road () is not a constant meaning,

2004-12-28 14:59:25 What is the value? () UNKNOWN & UNDEFINE?

2004-12-28 14:59:45 lalaala () Don't say that I wrote, let people joke, this is my turn

2004-12-28 15:00:00 icoding () lalalaconst int a = 0; int * p = (int *) & a; * p = 1; do not pinch this?

2004-12-28 15:00:14 lalala () can

2004-12-28 15:00:18 Walking the End of the World () What is the value of unknown & undefine? What is it?

2004-12-28 15:00:28 Once future () multi-logical

2004-12-28 15:00:46. Theash () not only doesn't know, but not defined

2004-12-28 15:01:26 What is the multi-logical pointing?

2004-12-28 15:01:46 icoding () The volatile doesn't have much relationship, but it is only lie to the compiler. The compiler doesn't know what P pointing is [; P]

2004-12-28 15:03:08 lalala () int main (int Argc, char * argv []) {const I = 0; int * p = (int *) & a; * p = 1; cout << a << endl; cout << * p << endl; system ("pause"); return 0;}

2004-12-28 15:03:15 Lalala () Output 0,1

2004-12-28 15:03:36 Wan .ash () bad programming style

2004-12-28 15:04:04 Lalala () int main (int Argc, char * argv []) {volatile const Int a = 0; int * p = (int *) & a; * p = 1; cout <

2004-12-28 15:04:10 lalaala () is 1, 1

2004-12-28 15:04:22 lalala () has volatile2004-12-28 15:04:28. As long as the forced conversion is used, no const grays are useless.

2004-12-28 15:04:45 lalaala () is 1, 1

2004-12-28 15:04:46 iCoding () [; @] How?

2004-12-28 15:04:52 Lalala (3) Const can only prevent you accidentally

2004-12-28 15:04:59 icoding (5) nnd ... I will try

2004-12-28 15:05:33 lalaala () You can change it, of course, can change

2004-12-28 15:07:10 Ricky (1) I remember that I was discussed in the group and a netizen.

2004-12-28 15:07:48 lalala () I also saw it, saying is the first case output 0, 1

2004-12-28 15:07:57 icoding () did not hear ...

2004-12-28 15:08:42 Lalala () is the case that did not add Volatile, the compiler retroses the constant with immediate

2004-12-28 15:09:07 ricky () Here you can say is an abuse of Volatile

2004-12-28 15:09:11 Walking up the definite words of the End of the End of the End () Volatile, it tells the compiler, and the variable can be modified by some things other than the application itself. These things include operating systems, hardware or simultaneous Performed thread, etc. In particular, the Volatile limiting word will tell the compiler, not to optimize the variable, and always reload the value of the memory cell from the variable.

2004-12-28 15:09:54 Unfeng () Volatile is just a hint for the compiler,

2004-12-28 15:10:00 icoding () const I = 1; int * p = (int *) & i; cout << i << endl; cout << * p << endl;

2004-12-28 15:10:16 icoding () Program Output: 11

2004-12-28 15:10:23 icoding () faint

2004-12-28 15:10:34 What is halo?

2004-12-28 15:10:42 lalaala () you didn't change,

2004-12-28 15:10:53 icoding () did not modify the pointer pointing value

2004-12-28 15:10:56 ricky () When a variable is defined as const, whether it is already implied: I don't want to change this variable, since this, I also use volatile to modify ... So I think it is Abuse

2004-12-28 15:11:04 lalala () const Int a = 0; int * p = (int *) & a; * p = 1;

2004-12-28 15:11:28. What did you explain this?

2004-12-28 15:11:31 lalaala () Do you say that volatile is pants fart?

2004-12-28 15:11:55 icoding () 12

2004-12-28 15:12:08 icoding () const I = 1; int * p = (int *) & i; * p = 2; cout << i << endl; cout << * p << endl ;

2004-12-28 15:12:34 Ricky (1) Of course not, in multi-threaded programming, in order to avoid error optimization, it is

2004-12-28 15:13:13 Lalala () is this: Volatile Limit The word will tell the compiler, do not optimize the variable and always reload the value of the memory cell from the variable. 2004-12-28 15:13:23 lalaala () We used him this feature

2004-12-28 15:13:26 .ash () is obviously abused, it is a constant thing, you still say not to optimize the compiler, what is the meaning of 捏? But use in the class to debut? ? ? Is it possible to make a Mutable variable not optimized?

2004-12-28 15:13:42 ricky () NOD, this is what I said

2004-12-28 15:14:28. Theash () But use in the class to debut? ? ? Is it possible to make a Mutable variable not optimized? Is it?

2004-12-28 15:15:08 ricky () ASH please explain

2004-12-28 15:15:11 Lalala () I don't know how to use Volatile normally, I haven't encountered it, which one is given an example

2004-12-28 15:16:06. Theash () such as the variable properties in a constant object, is it meaningful to him?

2004-12-28 15:18:31 Lalala () (Na.ash) 15:16:06, variable attributes in a constant object, is it meaningful to him with volatile?

Is it asked?

2004-12-28 15:18:44 Na.ash ()

2004-12-28 15:19:20 I haven't studied in the future () I haven't studied, usually I have jumped this kind of thing.

2004-12-28 15:19:25 Lalala (3) Since it is a constant object, there will be variable properties.

2004-12-28 15:19:55 Once future () C is not Java, you can't pay the price safely

2004-12-28 15:19:58 ricky () Mutable is not a variable property?

2004-12-28 15:20:00 ricky () just saw a paragraph in 9cbs, is being digested

2004-12-28 15:20:01. Theash () of course

2004-12-28 15:20:12 lalaala () posted

2004-12-28 15:20:09 icoding () I will move the stone to my feet MAD

2004-12-28 15:24:56 ricky () 4. Talking about Volatile and "Full Const Object" A class with Volatile modified only allows access to a subset of its interface, which controls the implementation of the class Users only use const_cast to access all of this type of interface. And like const, the class's Volatile property will pass to its member. Imagine constivated objects, its member variable is not modified, and it passes The object or native variable of the pointer maintenance is modified. Then we think: If the object maintains a char *, it is equivalent to char * const chrptr; not const char * cosnt chrptr; for the pointer in the class, you need to modify this It or its maintenance resources: cosnt x * xptr; rather than x * const xptr; because COSNT modified object it default behavior is continuation variable: x * cosnt xptr;

More importantly, Volatile modified data, the compiler cannot perform the optimization of the register in the execution period. This feature is for multi-threaded synchronization. Interests are visiting the ANDREI's GP series articles.

Http://www.chinaunix.net/jh/23/94481.html2004-12-28 15:26:12 ricky () About the volatile keyword instructions and tests: http://blog.vckbase.com/iwaswzq/ Archive / 2004/12/05 / 1896.aspx

2004-12-28 15:26:38 ricky () The last day is also a const question with Lalala?

2004-12-28 15:26:58 lalaala () I am learning with you, huh, huh

2004-12-28 15:27:11 Lalala (3) I am learning with you, huh, huh

2004-12-28 15:27:14 ricky () study together

2004-12-28 15:27:49 icoding () 虚 若 若 谷 .... PF Oh

2004-12-28 15:28:32 ricky () less joke, I will be OK.

2004-12-28 15:29:31 iCoding () Where? Who dares to jump out ~~~~~

are not there? are not there?

Oh, really dripping .....

2004-12-28 15:29:45 icoding () ohh

2004-12-28 15:31:13 iCoding () But I think it is still a C language conversion method easier to understand, C const_cast <> I don't understand ...

2004-12-28 15:32:11 ricky () C conversion method can enter a few letters, this is a little appreciation, otherwise I still use const_cast <>

2004-12-28 15:32:39 Lalala () 26367948 (Na.ash) 15:16:06 variable attribute in a constant object, is it meaningful to him with volatile?

Is the link to show me?

2004-12-28 15:32:56 icoding () ASH walking ~~~

Description and test of about volatile keyword: September eagles

The volatile keyword is a type modifier that uses its declared type variable representation that can be changed by some compilers, such as operating systems, hardware, or other threads. If this keyword declared variable, the compiler will no longer optimize the code to access the variable, so that stable access to special addresses can be provided. Examples of using this key are as follows:

Int Volatile NVINT;

When the value of the variable declared using the Volatile declaration, the system always re-reads data from its memory, even if its instructions have just read data from the point. And the read data is immediately saved. E.g:

Volatile INT i = 10;

INT a = i;

...

/ / Other code, did not clearly tell the compiler, to operate I

INT b = i;

Volatile indicates that I can change at any time. When using it, you must read from the address of i, so the assembly code generated by the compiler will re-read data from the address of i. The optimization method is that since the compiler found that the code between the code from the i-read data did not perform the operation, it automatically placed the last read data in B. Instead of re-reading. This way, if i is a register variable or indicates that a port data is easily error, so Volatile can guarantee stable access to special addresses. Note that in VC6, the general debug mode does not perform code optimization, so this keyword does not see. By inserting assembly code, testing there is no Volatile keyword, impact on the final code: First, build a Win32 Console project with ClassWizard, insert a Voltest.cpp file, enter the following code: #include

void main ()

{

INT i = 10;

INT a = i;

Printf ("i =% d / n", a);

/ / The role of the following assembled statement is to change the value of I in memory, but do not let the compiler know

__ASM {

MOV DWORD PTR [EBP-4], 20H

}

INT b = i;

Printf ("i =% d / n", b);

}

Then, run the program in the debug version mode, the output is as follows:

i = 10

i = 32

Then, run the program in the Release version mode, the output is as follows:

i = 10

i = 10

The results of the output show that in Release mode, the compiler optimizes the code, and the correct I value is not output for the second time. Below, we add the statement of i with the volatile keyword to see what changes:

#include

void main ()

{

Volatile INT i = 10;

INT a = i;

Printf ("i =% d / n", a);

__ASM {

MOV DWORD PTR [EBP-4], 20H

}

INT b = i;

Printf ("i =% d / n", b);

}

Run the program in the debug version and the release version, and the output is:

i = 10

i = 32

This shows that this keyplay has played its role! iWaswzq at 2004/12/4/16: 15: http://www.vckbase.com/document/viewdoc/?id = 1312


New Post(0)