Volatile variable

前端 未结 6 1496
梦谈多话
梦谈多话 2020-12-25 15:12

Where is a volatile variable stored in the program memory(in which section) ?

相关标签:
6条回答
  • 2020-12-25 15:37

    In C volatile just tells the compiler - "You don't have enough knowledge to assume the value of this variable hasn't changed". There is no "section" eg BSS, CSS for it.

    Consider it a flag to the compiler to prevent certain types of optimisations. Its very handy in embedded programming, where memory at a certain address may "change" due to a hardware device input.

    Here's a good explanation: http://www.embedded.com/columns/programmingpointers/174300478?_requestid=137658

    0 讨论(0)
  • 2020-12-25 15:37

    There's no reason for a volatile variable to be stored in any "special" section of memory. It is normally stored together with any other variables, including non-volatile ones. If some compiler decides to store volatile variables in some special section of memory - there's nothing to prevent it from doing so. But at the language level there's absolutely no reason for this.

    Why are you asking such a question? What made you think that it should be stored in some special section of memory?

    0 讨论(0)
  • 2020-12-25 15:48

    The volatility of a variable does not change the place in which a variable is stored. What it changes is the semantics around how it is accessed with respect to reads and writes.

    I do not believe the C standard says anything about the implementation of volatile. But typically, volatile guarantees release semantics for write operations on a variable and aquire semantics on read operations of a variable. This will not be true for every implementation though and you should read up on what your specific compiler guarantees

    0 讨论(0)
  • 2020-12-25 15:49

    volatile is a type qualifier not a storage class specifier, so it does not determine storage location at all; it affects the definition of a variable's type, not its storage.

    It simply forces the compiler to explicitly read a variable whose type is volatile from the variable's storage location (wherever that may be) rather than assuming that some previously read value in a register for example remains valid.

    0 讨论(0)
  • 2020-12-25 15:55

    volatile has nothing to deal with storage class.

    volatile just tells the compiler or force the compiler to "not to do the optimization" for that variable. so compiler would not optimize the code for that variable and reading the value from the specified location, not through interal register which holds the previous value.

    So, by declaring variable as volatile.. it gives garrantee that you will get the latest value, which may be alterred by an external event.

    your code may be work fine if haven't declare that variable as volatile, but there may be chance of not getting correct value sometimes.. so to avoid that we should declare variable as volatile.

    volatile is generally used when dealing with external events, like interrupts of hardware related pins.

    example. reading adc values.

    const voltile means you can not modify or alter the value of that variable in code. only external event can change the value.

    controller pins are generally defines as volatile. may be by declaring variable as volatile controller will do "read by pin" not "read by latch"... this is my assumtion. may be wrong...

    but still there is lots of confusion when to choose variable as volatile..

    A variable should be declared volatile whenever its value could change unexpectedly. In practice, only three types of variables could change:

    1. Memory-mapped peripheral registers

    2. Global variables modified by an interrupt service routine

    3. Global variables within a multi-threaded application

    Link : http://eetimes.com/discussion/beginner-s-corner/4023801/Introduction-to-the-Volatile-Keyword

    So It is proffered to variable as volatile in such cases.

    0 讨论(0)
  • 2020-12-25 15:58

    "Volatile" was used in C/C++ specifications to allow use of memory mapped devices. It directs the compiler not to optimize the variable defined with this keyword, just because the variable doesn't seem to change its state in compiler-visible code.

    0 讨论(0)
提交回复
热议问题