what are the implications of volatile....

This is a discussion on what are the implications of volatile.... within the C Programming forums, part of the General Programming Boards category; Hi, Code: unsigned int value=0; //Following is my declarations //Following is my timer handler which will be executed every 0.2 ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    139

    what are the implications of volatile....

    Hi,

    Code:
    unsigned int value=0; //Following is my declarations
    
    //Following is my timer handler which will be executed every 0.2 seconds
    
    void Timer_handler(void* pvMsg, uint8 u8MsgLen)
    {
      ///To check the volitality of the variable
       if(value<254)
       {
           vPrintf("\r\ncheck the volitality value :%d",value);
           value++;
       }
       else
       {
            value = 0;
       }
       once = TRUE;
       //Will create a timer
       (void)CreateTimer(Timer_handler, &u8Msg1, 0, 200, &u8TimerId1);
    }
    Here i have declared the variable "value" as normal automatic varibale.

    and the ouput is :

    1
    2

    Why is it that the compiler not optimising the variable?

    Isn't this(timer handler) main usage of volatile variable?

    Thanks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    What do you mean "not optimising".

    There's no reason for you to declare value as volatile in this code. The compiler is able to see where the variable is modified, so it is able to do the right thing.

    Your once variable on the other hand is different.

    Code:
    int once = 0;
    
    int main ( ) {
      (void)CreateTimer(Timer_handler, &u8Msg1, 0, 200, &u8TimerId1);
      while ( !once ) {
      }
    }
    Making once volatile would be a good idea, since the compiler cannot detect any modification of once.
    Without the qualifier, the compiler might generate while(1) instead, based on the known information at compile time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    139
    Quote Originally Posted by Salem View Post
    The compiler is able to see where the variable is modified, so it is able to do the right thing.
    I didn't get this point

    By changing the code to mode didn't work eithier

    Code:
    unsigned int once =0; //Following is my declarations
    
    //Following is my timer handler which will be executed every 0.2 seconds
    void Timer_handler(void* pvMsg, uint8 u8MsgLen)
    {
       vPrintf("\r\ncheck the volitality in sw_later once :%d",once); 
       once = 1;
       vPrintf("\r\ncheck the volitality in later once :%d",once);
       //Will create a timer
       (void)CreateTimer(Timer_handler, &u8Msg1, 0, 200, &u8TimerId1);
    }
    
    main()
    {
     (void)CreateTimer(Timer_handler, &u8Msg1, 0, 200, &u8TimerId1);
    while(1)
     if(once)
     {
        switch(State)
        {
            case TX_ST:
            //SendData();
            State = RX_ST;
            break;
            default:
            vPrintf("\n\rdefault");
            break ;
         }
         once = 0;  Is this what you mean by detecting modifications ?
         
      }
    }
    Ouput :
    check the volitality in sw_later once = 0
    check the volitality in later once = 1

    My code survied without being declared "once"as volatile.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > vPrintf("\r\ncheck the volitality in sw_later once :%d",once);
    It's not going to remember "in a special place" the old value of once, when re-invoking the interrupt handler.

    It only matters in cases like this
    Code:
    while ( once ) {
      printf("%d\n", once );
    }
    Here, once is 'read-only' for the duration of the loop, so the compiler is free to copy once to some "special place" as an optimisation, and thus lead to incorrect assumptions.

    By declaring it volatile (in this case), you force the compiler to always check the variable, and not assume that it is constant from inspecting the code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    139
    Where will the optimised variables stored?

    Will they be stored in registers(for microcontrollers) or the cache memory(in highend Processors)?If so the registers are limited to store all the optimised variables?

    Thanks in advance

  6. #6
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    OSdev is not the way to learn a language. Get a basic understanding, of both processor architecture, and coding, first.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Optimized values are usually just hard-coded numbers in the instructions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multithreading (flag stopping a thread, ring buffer) volatile
    By ShwangShwing in forum C Programming
    Replies: 3
    Last Post: 05-19-2009, 07:27 AM
  2. strange grammar about volatile and operator overload
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 01-30-2008, 04:38 AM
  3. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  4. volatile int address ??
    By pprabhakar in forum C Programming
    Replies: 2
    Last Post: 05-14-2006, 01:37 PM
  5. volatile keyword help!!!!
    By penney in forum Linux Programming
    Replies: 2
    Last Post: 03-12-2003, 07:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21