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.

need to more explanation about volatile

Status
Not open for further replies.

selva murugesan

Advanced Member level 4
Full Member level 1
Joined
Mar 30, 2012
Messages
116
Helped
2
Reputation
4
Reaction score
1
Trophy points
1,298
Visit site
Activity points
2,007
i have to write the c code as it returns 2 value simultaneously. what are the possible way to do this?can u explain with example?
and second question is full usage volatile keyword in c and c++?
 

Hi pass the addresses of the two return values
like

Code C - [expand]
1
void divide(short nominator, short denominator, short *quotient, short *reminder);



volatile is used for telling the compiler this lines are hardware registers they can change any time so dont optimize this variable....

- - - Updated - - -


Code C - [expand]
1
2
3
4
5
void divide(short nominator, short nominator, short *quotient, short *reminder);
{
*quotient = nominator / nominator;
*reminder = nominator / nominator;
}

 

Not sure about those C examples... You probably want to pass the addresses and use different parameter names.

"volatile" in many case does nothing at all. It is an instruction to the compiler not to optimize the code including it. For example, without it there might be a risk that the compiler thinks the code is redundant and may remove it or change it in a way that may make sense in programming terms but would stop it working as you intend it to. Adding the word 'volatile' tells the compiler to use it exactly as it is.

Brian.
 

thanks for reply

i need more explanation about volatile keyword, volatile pointer, what exactly happens inside memory while using this keyword?
 

A volatile variable or address sits in memory exactly like any other variable. It doesn't change the operation of the program unless any memory management software is dynamically allocating address space. It's a hint to the compiler that it isn't safe to ignore it or share it's allocation with anythng else.

Simple example:

volatile int MyVolatileInt;
int My_Non_VolatileInt;

main()
{
MyVolatileInt++;
}

Most compilers will completely ignore "My_Non_VolatileInt" because it isn't used anywhere in the program, it will not have any memory space allocated to it. If it really is needed, for example in an external function, stating it is volatile will force the compiler to reserve space for it even though it doesn't seem to do anything.

Brian.
 
The discussion above is true in a broad, definitive sense. In the micro-controller world and with PC I/O, the most common use of Volatile in embedded programming is with a "write only" output port register. Without the volatile designation, the compiler might throw away any use of the variable since it is never read and seemingly not needed. So, in practice, it is a common way of specifying to a compiler that the variable is really a bit of hardware and optimizations should not be applied to it. If you look at the header files (.h) which define I/O ports for many micro-controllers, the volatile qualifier is used throughout. Back in the old days, this was about the only use of the qualifier when using C. With threads and so forth, the use has expanded. Anyway, I post this to specifically highlight the use with programming I/O.
 
Suppose we have an 8 bit output port memory mapped at address 0xabcd, then we can usually (it is highly system dependent) write something like:

volatile unsigned char *port = (volatile unsigned char *) 0xabcd;

Then we could pulse bit 0 of the port with something like:
*port = 0;
*port = 1;
*port = 0;

Without the volatile keyword the compiler would be free to optimise all of this to nothing at all as the variable port is only ever written, and thus has no effect on the execution of the program from the POV of the compiler.

HTH.

Regards, Dan.
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top