Pointer declared as constant as well as volatile

前端 未结 6 995
北海茫月
北海茫月 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:43

    const doesn't make a variable constant. It just make the compiler refuse some write access. This is still possible to write in the variable (via const-casted pointers for instance).

    You can see const as a protection against coding mistakes.

    This declaration make sure you don't inadvertently write to p, while telling the compiler not to optimize accesses (cache, out of order execution(?),...) because an external event may write in p.

提交回复
热议问题