volatile and constant

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Geevi
    New Member
    • Jan 2007
    • 13

    volatile and constant

    Hi all

    what is meant by volatile and const?

    wher they can be used?
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Originally posted by Geevi
    Hi all

    what is meant by volatile and const?

    wher they can be used?
    The keyword const in a definition or declaration indicates to the compiler that once the object is initialised it is to be treated as a constant and may not be altered.

    The keyword volatile iindicates that the contents of the object are subject to unpredictable alterations and references to it must not be optimised. For example, it may be a variable which will be changed by an interrupt service routine, or an I/O register which will be changed by the action of the corresponding I/O device. If a volatile I/O register should not be changed by the program it can also be type modified with const, e.g.:
    Code:
    const volatile unsigned char io_register;                 // a byte sized I/O register
    This declares a byte sized object io_register which may not be altered by the program but is subject to changes from outside the program.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      the const keyword creates a variable that can't be altered, i.e. has a fixed or constant value. In the case of a pointer it can create a pointer that can't be altered or a pointer to something that can't be altered.

      Code:
      const int PI1000 = 3141;   /* Constant integer */
      const int *pcInt = &PI1000; /* pointer to an integer that is constant, the pointer can be changed but the value of the integer it points to can't */
      const int const *pcInt = &PI1000; /* Constant pointer to an integer that is constant, the pointer can't be changed and neither can the value of the integer it points */
      int x;
      int *pcInt = &x; /* Constant pointer to an integer, the pointer can't be changed but the value of the integer it points to can be changed */
      The volatile keyword marks a variable about which the compiler can make no assumptions. This means that the compiler can not optomise uses of the variable. It would be used in the situation where you have a pointer to a memory location that could be changed outside the control of the main processor (say the receive register location of a UART).

      I have mainly seen it used with pointers, so pointer to a volotile unsigned integer

      volatile unsigned int *UART_RX_REGIST ER;

      Comment

      • Geevi
        New Member
        • Jan 2007
        • 13

        #4
        Originally posted by horace1
        The keyword const in a definition or declaration indicates to the compiler that once the object is initialised it is to be treated as a constant and may not be altered.

        The keyword volatile iindicates that the contents of the object are subject to unpredictable alterations and references to it must not be optimised. For example, it may be a variable which will be changed by an interrupt service routine, or an I/O register which will be changed by the action of the corresponding I/O device. If a volatile I/O register should not be changed by the program it can also be type modified with const, e.g.:
        Code:
        const volatile unsigned char io_register;                 // a byte sized I/O register
        This declares a byte sized object io_register which may not be altered by the program but is subject to changes from outside the program.

        Thnks a lot...

        Comment

        Working...