Is the definition of “volatile” this volatile, or is GCC having some standard compliancy problems?

前端 未结 4 1503
醉酒成梦
醉酒成梦 2020-12-13 01:32

I need a function that (like SecureZeroMemory from the WinAPI) always zeros memory and doesn\'t get optimized away, even if the compiler thinks the memory is never going to

4条回答
  •  情歌与酒
    2020-12-13 01:54

    I need a function that (like SecureZeroMemory from the WinAPI) always zeros memory and doesn't get optimized away,

    This is what the standard function memset_s is for.


    As to whether this behavior with volatile is conforming or not, that's a bit hard to say, and volatile has been said to have long been plagued with bugs.

    One issue is that the specs say that "Accesses to volatile objects are evaluated strictly according to the rules of the abstract machine." But that only refers to 'volatile objects', not accessing a non-volatile object via a pointer that has had volatile added. So apparently if a compiler can tell that you're not really accessing a volatile object then it's not required to treat the object as volatile after all.

提交回复
热议问题