Pointer declared as constant as well as volatile

前端 未结 6 948
北海茫月
北海茫月 2021-01-31 07:04

While reading I came across this type of declaration and the following line -

const volatile char *p=(const volatile char *) 0x30;
6条回答
  •  梦如初夏
    2021-01-31 07:45

    The const says that the flow of your program isn't going to modify what is pointed to by p. Any attempt to modify the value after dereferencing the pointer will result in a compile-time error:

    *p = 'A'; // will not compile
    

    Note that this isn't a particularly strong contract; the value at location 0x30 can still be changed through an aliasing non-const pointer, other than p:

    volatile char *q = 0x30;
    *q = 'A'; // will compile
    

    Another way to break this contract is by casting away the const from p:

    *(volatile char *) p = 'A'; // will compile
    

    The volatile, however, doesn't exclude any modifications which could be caused by another thread, the kernel, an asynchronous signal handler or an external device which has access to the same memory space. This way the compiler cannot make the wrong assumption that the value pointed to by p doesn't change and will load it from memory every time it is referenced:

    /*
     The character at 0x30 will be read on every iteration,
     even if the compiler has proven that the program itself
     doesn't modify the value at that address.
    */
    while (*p) {
        ...
    }
    

    If the compiler was to erroneously optimise such a construct, it could emit instructions which load the value only once from memory and then keep it in a register. The register is essentially an independent copy and any changes to the original location will not reflect there, and, needless to say, this can cause some very nasty bugs.

提交回复
热议问题