我发现在有些头文件中,声明的一些变量前面加了volatile关键词,比如:
volatile long cNum=0;
他和long cNum=0;有什么区别呢??? 我觉得他们都是全局变量!!!
希望各位大哥帮忙解释, 肯定给分!!!

解决方案 »

  1.   

    volatile declaratorThe volatile keyword is a type qualifier used to declare that an object can be modified in the program by something other than statements, such as the operating system, the hardware, or a concurrently executing thread.The following example declares a volatile integer nVint whose value can be modified by external processes:int volatile nVint;
    Objects declared as volatile are not used in optimizations because their value can change at any time. The system always reads the current value of a volatile object at the point it is requested, even if the previous instruction asked for a value from the same object. Also, the value of the object is written immediately on assignment. One use of the volatile qualifier is to provide access to memory locations used by asynchronous processes such as interrupt handlers.
      

  2.   

    volatile 表示这个变量可能被意外改变,
    比如被中断或者操作系统在你的程序不知道的时候修改。
    它要求编译器不要对有关这个变量的代码进行优化。比如long flag=1;
    int main()
    {
     long sum=0;
     for(int i=0;i<10000;i++)
     sum+=flag;
     printf("sum=%d",sum);
     return 0;
    }假设在循环到 5000次的时候,falg变成了2.
    上面的程序结果将仍然是 10000,而不是15000
    因为编译器会把flag优化成一个寄存器(可能是AX),
    而不会真正去读取flag的值。
    如果
    volatile long flag=1;
    就能得到正确结果.详细见MSDN.
      

  3.   

    iicup(双杯献酒) 解释的很有道理;这个关键字在<<thinking in C++ >>一书有一章较详细的论述之;不明白的去看看;看E文我头疼
      

  4.   

    const and volatile Pointers
    The const and volatile keywords change how pointers are treated. The const keyword specifies that the pointer cannot be modified after initialization; the pointer is protected from modification thereafter.The volatile keyword specifies that the value associated with the name that follows can be modified by actions other than those in the user application. Therefore, the volatile keyword is useful for declaring objects in shared memory that can be accessed by multiple processes or global data areas used for communication with interrupt service routines.When a name is declared as volatile, the compiler reloads the value from memory each time it is accessed by the program. This dramatically reduces the possible optimizations. However, when the state of an object can change unexpectedly, it is the only way to ensure predictable program performance.To declare the object pointed to by the pointer as const or volatile, use a declaration of the form:const    char *cpch;
    volatile char *vpch;To declare the value of the pointer — that is, the actual address stored in the pointer — as const or volatile, use a declaration of the form:char * const    pchc;
    char * volatile pchv;The C++ language prevents assignments that would allow modification of an object or pointer declared as const. Such assignments would remove the information that the object or pointer was declared with, thereby violating the intent of the original declaration. Consider the following declarations:const char cch = 'A';
    char        ch = 'B';Given the preceding declarations of two objects (cch, of type const char, and ch, of type char), the following declaration/initializations are valid:const char        *pch1 = &cch;
    const char *const  pch4 = &cch;
    const char        *pch5 = &ch;
    char              *pch6 = &ch;
    char       *const  pch7 = &ch;
    const char *const  pch8 = &ch;The following declaration/initializations are erroneous.char *pch2 = &cch;        // Error
    char *const pch3 = &cch;  // ErrorThe declaration of pch2 declares a pointer through which a constant object might be modified and is therefore disallowed. The declaration of pch3 specifies that the pointer is constant, not the object; the declaration is disallowed for the same reason the pch2 declaration is disallowed.The following eight assignments show assigning through pointer and changing of pointer value for the preceding declarations; for now, assume that the initialization was correct for pch1 through pch8.*pch1 = 'A';  // Error: object declared const
    pch1 = &ch;   // OK: pointer not declared const
    *pch2 = 'A';  // OK: normal pointer
    pch2 = &ch;   // OK: normal pointer
    *pch3 = 'A';  // OK: object not declared const
    pch3 = &ch;   // Error: pointer declared const
    *pch4 = 'A';  // Error: object declared const
    pch4 = &ch;   // Error: pointer declared constPointers declared as volatile or as a mixture of const and volatile obey the same rules.Pointers to const objects are often used in function declarations as follows:char *strcpy( char *szTarget, const char *szSource );The preceding statement declares a function, strcpy, that takes two arguments of type “pointer to char” and returns a pointer to type char. Because the arguments are passed by reference and not by value, the function would be free to modify both szTarget and szSource if szSource were not declared as const. The declaration of szSource as const assures the caller that szSource cannot be changed by the called function.Note   Because there is a standard conversion from typename * to const typename *, it is legal to pass an argument of type char * to strcpy. However, the reverse is not true; no implicit conversion exists to remove the const attribute from an object or pointer.A const pointer of a given type can be assigned to a pointer of the same type. However, a pointer that is not const cannot be assigned to a const pointer. The following code shows correct and incorrect assignments:int *const cpObject = 0;
    int *pObject;void main()
    {
        pObject = cpObject; // OK
        cpObject = pObject; // Error
    }
      

  5.   

    Type Qualifiers 
    Type qualifiers give one of two properties to an identifier. The const type qualifier declares an object to be nonmodifiable. The volatile type qualifier declares an item whose value can legitimately be changed by something beyond the control of the program in which it appears, such as a concurrently executing thread. The two type qualifiers, const and volatile, can appear only once in a declaration. Type qualifiers can appear with any type specifier; however, they cannot appear after the first comma in a multiple item declaration. For example, the following declarations are legal:typedef volatile int VI;
    const int ci;These declarations are not legal:typedef int *i, volatile *vi;
    float f, const cf;   Type qualifiers are relevant only when accessing identifiers as l-values in expressions. See L-Value and R-Value Expressions in Chapter 4 for information about l-values and expressions. Syntaxtype-qualifier :const
    volatileThe following are legal const and volatile declarations: int const *p_ci;       /* Pointer to constant int */
    int const (*p_ci);     /* Pointer to constant int */
    int *const cp_i;       /* Constant pointer to int */
    int (*const cp_i);     /* Constant pointer to int */
    int volatile vint;     /* Volatile integer        */If the specification of an array type includes type qualifiers, the element is qualified, not the array type. If the specification of the function type includes qualifiers, the behavior is undefined. Neither volatile nor const affects the range of values or arithmetic properties of the object.This list describes how to use const and volatile. The const keyword can be used to modify any fundamental or aggregate type, or a pointer to an object of any type, or a typedef. If an item is declared with only the const type qualifier, its type is taken to be const int. A const variable can be initialized or can be placed in a read-only region of storage. The const keyword is useful for declaring pointers to const since this requires the function not to change the pointer in any way. 
    The compiler assumes that, at any point in the program, a volatile variable can be accessed by an unknown process that uses or modifies its value. Therefore, regardless of the optimizations specified on the command line, the code for each assignment to or reference of a volatile variable must be generated even if it appears to have no effect. 
    If volatile is used alone, int is assumed. The volatile type specifier can be used to provide reliable access to special memory locations. Use volatile with data objects that may be accessed or altered by signal handlers, by concurrently executing programs, or by special hardware such as memory-mapped I/O control registers. You can declare a variable as volatile for its lifetime, or you can cast a single reference to be volatile. An item can be both const and volatile, in which case the item could not be legitimately modified by its own program, but could be modified by some asynchronous process. 
      

  6.   

    u can see ,the most important character of "volatile" is that it can be changed by other code beyond the control of the program in which it appears....Do u understand ? :)
      

  7.   

    哇e文阿.頭疼..
    volatile表示你可以在其他進程對他的值進行修改