What's the difference of the usage of volatile between C/C++ and C#/Java?

后端 未结 3 534
盖世英雄少女心
盖世英雄少女心 2020-12-23 18:10

I found it in many references which mention that volatile in C/C++ is is weak and may cause issue in concurrent environment on multiple processor, but it (

3条回答
  •  醉酒成梦
    2020-12-23 18:51

    For C#/Java, "volatile" tells the compiler that the value of a variable must never be cached as its value may change outside of the scope of the program itself. The compiler will then avoid any optimisations that may result in problems if the variable changes "outside of its control".

    In C/C++, "volatile" is needed when developing embedded systems or device drivers, where you need to read or write a memory-mapped hardware device. The contents of a particular device register could change at any time, so you need the "volatile" keyword to ensure that such accesses aren't optimised away by the compiler.

提交回复
热议问题