Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

What action does a compiler take if we declare a variable as a volatile?

Status
Not open for further replies.

sacrpio

Member level 3
Joined
May 24, 2004
Messages
56
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
500
What action compiler takes if we declare a variable as a volatile. Please tell in terms of complier actions.

Thanks in advance
 

Re: Volatile Keyword

Volatile means that a variable can be altered from an external source (like interrupt) and the optimizer of the compiler can not assume that the variable stays unchanged between accesses.

With microcontrollers for example the ports are declared as volatile in the header file of a processor.

This is from the Hi-Tech C manual:
The volatile type qualifier is used to tell the compiler that an object cannot be guaranteed to retain its
value between successive accesses. This prevents the optimizer from eliminating apparently redundant
references to objects declared volatile because it may alter the behaviour of the program to do so. All
Input/Output ports and any variables which may be modified by interrupt routines should be declared
volatile, for example:
volatile unsigned char P_A @ 0x05;

hope this helps and best regards
 
Volatile Keyword

Have a look here:

Introduction to the Volatile Keyword
**broken link removed**

/Bingo
 

Re: Volatile Keyword

Verry exact explanation from C-Man!!

I wish only to add this on C-Man post:

Dont abuse the volatile keyword!!

At first glance you are tempted to declare all variables in your program volatile (because you can use them safelly in interupts, for example)

Every time you declare a variable volatile, the compiler reserve a location of memory only for that variable , this location cannot be used anymore by the overlay processor (wich can free memory resources when not nedeed).

On an embedeed processor with low RAM memory, the generated code will poorly optimized (read: low execution time)
 

Re: Volatile Keyword

Well my 2-cents:

#1 Compilers try to optimize your code as much as they can. In douing so they may chose to ignore certain statements you wrote simply because in their opinion they may be redundant. For example:

Code:
{
  int i;
  i = 10;
  i = 20;
  printf("\n%d", i);
}
clearly, the first assignment is redundant and the compiler will choose to ignore it when it optimizes the code.

#2 There may be situations in which such kind of optimizations are undesirable. For example the situation listed by C-Man. In general if you have a variable that may be modified from some other thread, then in such cases, optimizations should not be allowed. Now the question is how to instruct the compiler that optimization is not to be performed for this particular variable? Yes you guessed it! declare it volatile.
 

Volatile Keyword

check here:

**broken link removed**


there's a short part on explaining the effect of 'volatile'.
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top