What is the use of volatile keyword?

后端 未结 7 983
醉酒成梦
醉酒成梦 2020-12-04 23:05

What is the use of volatile keyword in C/C++? What is the difference between declaring a variable volatile and not declaring it as volatile?

7条回答
  •  悲&欢浪女
    2020-12-04 23:19

    Volatile tells the compiler that the variable might change without it knowing - so it shouldn't optimise it away.

    The only time I have ever needed it was in the days of ISA cards when you read a memory address to get the data from the bus. There was also a bug in the compiler which meant volatile didnt work!

    It can also be useful in some parallel / mutli-threaded code

提交回复
热议问题