Thread: When we should avoid use of volatile

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    66

    When we should avoid use of volatile

    volatile means value of variable may be change at anytime

    In example, compiler know that foo is set to 0 and compiler will never look into while loop but by external even when foo become 1 then compiler will execute statement

    Code:
    int foo
    void bar ()
    {
        foo = 0;
         while (foo = 1)
          {
              statement 
          }
    }
    When we should avoid use of volatile variable in program?

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    676
    In the example, foo is set to 1 and the while loop never ends.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    3,863
    Quote Originally Posted by Player777 View Post
    volatile means value of variable may be change at anytime


    When we should avoid use of volatile variable in program?
    You should only use volatile when it is needed.
    Newbies should avoid it nearly all the time!
    When dealing with hardware registers it is needed.
    When dealing with ISR routines it is often needed. Interrupt Service Routines(ISR).
    When dealing with dealing with multiple processes it might be needed.
    Edit: When dealing with shared memory it might be needed.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is use of volatile keyword in c
    By skyr6546 in forum C Programming
    Replies: 1
    Last Post: 11-20-2019, 09:49 AM
  2. To Volatile, or not to Volatile?
    By EVOEx in forum C++ Programming
    Replies: 16
    Last Post: 05-12-2012, 02:07 PM
  3. Volatile Variable.
    By nagavardna in forum C Programming
    Replies: 2
    Last Post: 04-19-2012, 03:34 AM
  4. Volatile in dos application
    By vaibhav in forum C++ Programming
    Replies: 2
    Last Post: 08-02-2006, 11:00 PM
  5. volatile??
    By jacktibet in forum C Programming
    Replies: 2
    Last Post: 05-29-2003, 03:46 PM

Tags for this Thread