Thursday, May 16, 2013

constant and volatile

  static means a variable will be globally known only in this file. extern means a global variable defined in another file will also be known in this file, and is also used for accessing functions defined in other files.

 

Please any one explain me about

      1. Global variable  (example :   int i_Global)
      2. Extern variable (example  :  extern  int i_Global)
      3. static variable  (example : static int i_Local means in .h file)
      4. static global variable ( example : static int i_Global)

  • 1. Defines a global variable, allocating memory for it which will be accessible by all files in the program.
    2. Declares a global variable which is defined in another file. No memory is allocated. This is how global variables defined in one file can be used in another.
    3. static local variables retain their values on repeated calls into the function unlike other local variables.
    4. These global variables can only be accessed from within the file. Using the static keyword you can declare global variables with the same name in multiple files in the same program.

 http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/15b55a9f-ee92-495a-a18c-9f5dc42cca96

http://stackoverflow.com/questions/3684450/what-is-the-difference-between-static-and-extern-in-c

 storage

  1. Local variables and function frame - stack
  2. Global and static variables if uninitialized - .bss block start by symbol
  3. Global and static variables if initialized - data segment
  4. Environment variables and arguments - on top of the stack
  5. Dynamic data allocation - heap
  6. Const - ROM
  7. Volatile - no storage
  8. Register - cpu register
  9. Const volatile - in the same place as const storage

 


In which section is constant volatile variable stored in c.? In micro-controllers we should put such kind of variables in RAM. Right?





4 Answers

up vote 3 down vote accepted
A const volatile variable means that your C program can't legally change it, but something else can. It would be logical to place this variable in RAM, but the compiler won't complain if you tell it (via a linker script or a similar option) to place in ROM. It may also be useful to locate this variable where some memory-mapped device is, e.g. a read-only timer counter register or an ADC output register.




So if we are assigning constant variables to ROM then compiler will automatically allocate such variables to ROM. right? – suraj May 4 '12 at 11:09

It depends on your compiler and how you use it. – Alexey Frunze May 4 '12 at 11:11

Volatile has noting to do with where the variable is stored. It just tells the compiler to read the variable from memory every time to avoid any optimization that compiler might perform for that variable.




const variables for microcontroller applications are most likely stored in flash ROM. The only time they are stored in RAM is when they are evaluated in runtime, such as const parameters to functions. Or when you are doing some debug build executing from RAM.
volatile has nothing to do with where variables are stored, as explained in other answers.

 

 

 

 

Difference between const & const volatile

http://stackoverflow.com/questions/4592762/difference-between-const-const-volatile

If we declare a variable as volatile every time the fresh value is updated
If we declare a variable as const then the value of that variable will not be changed
Then const volatile int temp;
What is the of declaring the variable temp as above?
What happens if we declare as const int temp?










3 Answers 

 

  • volatile will tell the compiler not to optimise code related the variable, usually when we know it can be changed from "outside", e.g. by another thread.
  • const will tell the compiler that it is forbidden for the program to modify the variable's value.
  • const volatile is a very special thing you'll probably see used exactly 0 times in your life (tm). As is to be expected, it means that the program cannot modify the variable's value, but the value can be modified from the outside, thus no optimisations will be performed on the variable.


 


An object marked as const volatile will not be permitted to be changed by the code (an error will be raised due to the const qualifier) - at least through that particular name/pointer.
The volatile part of the qualifier means that the compiler cannot optimize or reorder access to the object.
In an embedded system, this is typically used to access hardware registers that can be read and are updated by the hardware, but make no sense to write to (or might be an error to write to).
An example might be the status register for a serial port. Various bits will indicate if a character is waiting to be read or if the transmit register is ready to accept a new character (ie., - it's empty). Each read of this status register could result in a different value depending on what else has occurred in the serial port hardware.
It makes no sense to write to the status register (depending on the particular hardware spec), but you need to make sure that each read of the register results in an actual read of the hardware - using a cached value from a previous read won't tell you about changes in the hardware state.
A quick example:
unsigned int const volatile *status_reg; // assume these are assigned to point to the 
unsigned char const volatile *recv_reg;  //   correct hardware addresses


#define UART_CHAR_READY 0x00000001

int get_next_char()
{
    while ((*status_reg & UART_CHAR_READY) == 0) {
        // do nothing but spin
    }

    return *recv_reg;
}
If these pointers were not marked as being volatile, a couple problems might occur:
  • the while loop test might read the status register only once, since the compiler could assume that whatever it pointed to would never change (there's nothing in the while loop test or loop itself that could change it). If you entered the function when there was no character waiting in UART hardware, you might end up in an infinite loop that never stopped even when a character was received.
  • the read of the receive register could be moved by the compiler to before the while loop - again because there's nothing in the function that indicates that *recv_reg is changed by the loop, there's no reason it can't be read before entering the loop.
The volatile qualifiers ensures that these optimizations are not performed by the compiler.





+1 for explanation. And I have a question: what about const volatile methods? If I have a class, which is accessed by many threads (although access is synchronized with mutex) does my const methods also have to be volatile (since some variable could be changed by other thread) – Sasa Aug 8 '12 at 23:31



3  
I'd have thought that volatile variables are usually what happens when you start messing with hardware, not with other threads. Where I've seen const volatile used is in things like memory-mapped status registers or the like. – JUST MY correct OPINION Jan 4 '11 at 12:33

Of course, you're absolutely right, multithreading is just one example, but not the only one :). – mingos Jan 4 '11 at 15:16


It is not because the variable is const that it may not have changed between two sequence points.
Constness is a promise you make not to change the value, not that the value won't be changed.

No comments:

Post a Comment