costs of volatile

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is there any memory or processing cost or downside to declaring a variable volatile?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There must be!

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Of-course. No optimization is done on items being declared volatile. So "more" code will be generated and executed. Ie both memory cost and execution time cost.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can see this yourself:

#include 

int main(void) {
	uint8_t value = 0x5A;

	while(1) {
		PORTB = value;
	}
}

generates:

0000006c 
: int main(void) { uint8_t value = 0x5A; while(1) { PORTB = value; 6c: 8a e5 ldi r24, 0x5A ; 90 6e: 88 bb out 0x18, r24 ; 24 70: fe cf rjmp .-4 ; 0x6e

on the other hand:

int main(void) {
	volatile uint8_t value = 0x5A;

	while(1) {
		PORTB = value;
	}
}

generates:

0000006c 
: #include int main(void) { 6c: df 93 push r29 6e: cf 93 push r28 70: 0f 92 push r0 72: cd b7 in r28, 0x3d ; 61 74: de b7 in r29, 0x3e ; 62 volatile uint8_t value = 0x5A; 76: 8a e5 ldi r24, 0x5A ; 90 78: 89 83 std Y+1, r24 ; 0x01 while(1) { PORTB = value; 7a: 89 81 ldd r24, Y+1 ; 0x01 7c: 88 bb out 0x18, r24 ; 24 7e: fd cf rjmp .-6 ; 0x7a

Only use 'volatile' when you have to.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Only use 'volatile' when you have to.

The question is so generic. "Have you stopped beating your wife yet? Answer yes or no."

Is there a "cost" to not using volatile, when needed? Yes; your program will not operate correctly.

Is there a "cost" to declaring everything volatile? In all probability yes; you will cripple the optimizer.

Can you minimize the "cost" with some thought? Yes. Consider the below:

volatile unsigned char frog;
...
for (i = 0; i < frog; i++)
{
.. do something useful
}

with

volatile unsigned char frog;
unsigned char dog;
...
dog = frog;
for (i = 0; i < dog; i++)
{
.. do something useful
}

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:

volatile unsigned char frog;
unsigned char dog;
...
dog = frog;
for (i = 0; i < dog; i++)
{
.. do something useful
}

Of course, the whole purpose to declaring a variable volatile is to tell the compiler that "something" (usually either hardware or an ISR) can change the variable behind the compiler's back. So, in the code above, if frog changes while the processor is in the dog for loop, the loop will not see the change; the loop will continue to the value of frog immediately before the for loop was started.

Stu

Engineering seems to boil down to: Cheap. Fast. Good. Choose two. Sometimes choose only one.

Newbie? Be sure to read the thread Newbie? Start here!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Exactly, Stu. As implied by the example, the volatile variable is often from an ISR--ADC counts, ICP time, encoder counts. Often, the main loop acting on the information wants a snapshot and then go on and close the loop.

[But not always--it depends on >>your<< app!!]

The point is that in the simple loop above the volatile may cause an extra LDS each loop, and virtually doubles the loop overhead time. Continuing with the contrived example, perhaps frog is ADCH counts. And perhaps we toggle a pin to indicate that number. Say we start out with frog being 0x66 and we are using it directly as a volatile. Then when our i count is 0x44 it changes to 0x22. We have now acted on a value that is neither fish nor fowl.

It depends. OP's question cannot be answered yea or no and one size does not fit all.

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

dog = frog???
Are you people on drugs or something?

Four legs good, two legs bad, three legs stable.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Are you people on drugs or something?
ROFL

It may (or may not) be worth noting that these volatile rules-of-thumb are somewhat GCC specific. I know we are in the GCC forum but some of the young'ens might be interested that other compilers may behave differently with regard to optimization and volatile.

C: i = "told you so";

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

John_A_Brown wrote:
dog = frog???
Are you people on drugs or something?
:lol: :lol:

I figured there would have to be... moral of the story: if you need to use volatile to get your program to work right, use it. otherwise don't.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

More correctly, use volatile when it is required.