高分求一个变量声明的含义
const volatile int *a = (const volatile int *)0xfff8;
我的理解:
a是一个指针,指向一个int型数据, a被初始化为内存中0xfff8处的一个int单元,a指向的值随时可能会改变,但用户不能修改其值。
[解决办法]
对的。
[解决办法]
volatile表示可被别处(比如别的线程)修改,但这里不能修改它。
[解决办法]
没说清楚,是const表明这里不能修改它。
[解决办法]
应该没错,确切地讲是程序中不能改变*a,且a是可改变的
[解决办法]
a是一个指向常量的指针,a指向的地址中的值也许可以修改,但是
不能通过a直接进行修改, 一种可行的方法,使使用强制类型转换,
在c语言中强制类型转换就是老大,c++中也提供const_cast关键字
如 *(volatile int *)a = ...,或 * const_cast <volatile int *> (a) = ..
[解决办法]
const volatile int *a
=====================
一个一个的看,const在*左边,说明它修饰的是指针a所指向的对象
a可以指向不同的对象,但指向的每个的对象均不能修改
volatile类似在*的左边,是指指针a所指的对象
[解决办法]
The volatile Type Qualifier
The volatile qualifier tells the compiler that a variable can have its value altered by agencies other than the program. It is typically used for hardware addresses and for data shared with other programs running simultaneously. For example, an address might hold the current clock time. The value at that address changes as time changes, regardless of what your program is doing. Or an address could be used to receive information transmitted from, say, another computer.
The syntax is the same as for const:
volatile int loc1; /* loc1 is a volatile location */
volatile int * ploc; /* ploc points to a volatile location */
These statements declare loc1 to be a volatile value and ploc to point to a volatile value.
You may think that volatile is an interesting concept, but you might be wondering why the ANSI committee felt it necessary to make volatile a keyword. The reason is that it facilitates compiler optimization. Suppose, for example, you have code like this:
val1 = x;
/* some code not using x */
val2 = x;
A smart (optimizing) compiler might notice that you use x twice without changing its value. It would temporarily store the x value in a register. Then, when x is needed for val2, it can save time by reading the value from a register instead of from the original memory location. This procedure is called caching. Ordinarily, caching is a good optimization, but not if x is changed between the two statements by some other agency. If there were no volatile keyword, a compiler would have no way of knowing whether this might happen. Therefore, to be safe, the compiler couldn 't cache. That was the pre-ANSI situation. Now, however, if the volatile keyword is not used in the declaration, the compiler can assume that a value hasn 't changed between uses, and it can then attempt to optimize the code.
A value can be both const and volatile. For example, the hardware clock setting normally should not be changed by the program, making it const, but it is changed by an agency other than the program, making it volatile. Just use both qualifiers in the declaration, as shown here; the order doesn 't matter:
volatile const int loc;
const volatile int * ploc;
[解决办法]
这是Stephen 's C Primer Plus(5th Edition)里的原文。
中文理解:
volatile 类型限制符是告诉编译器我这个变量或者指针指向的空间是会有其它代理(其它进程或者硬件)修改。
这样做的目的是避免编译器做一些快存缓冲的优化,例如:
val1 = x;
/* some code not using x */
val2 = x;
如果x不是volatile,那么编译器(假设在两条语句执行期间x的值不会改变)会将x的值放在cach中这样val2 = x;执行时就直接从cach里取就是了。
但是如果是volatile,那么编译器就不会作上述优化。
当然如果你加上const,实际上在你的程序中也不能改变它的值了。