Volatile variables in structs

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

Is there a problem putting volatile variables in a struct that is not entirely volatile?

I.e.:

typedef struct {
	volatile uint8_t tick;
	uint8_t tick_happened;	
} timingVars_t;

extern timingVars_t timingStatus; (declared elsewhere)

tick is set in an ISR, but tick_happened is only set outside an ISR.

This compiles and seems to work, but I am looking for verification that this is actually doing what I think it is (and that if I change optimization settings, it won't spontaneously break). It doesn't SEEM like it should work.

Science is not consensus. Science is numbers.

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

It's fine. tick is a volatile object, as it would be in any other context with the volatile keyword. What makes you think it wouldn't be volatile because it's in a struct?

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

Maybe my understanding of how volatile (or maybe structs?) functions is flawed. As I understand it, the keyword volatile tells the compiler not to mess with the variable w/ regards to optimization (i.e., as the coder, I know the variable is used elsewhere, even if doesn't appear to be (say, in an ISR)). What happens if tick_happened gets optimized away, but tick doesn't? Does the struct still exist as an entity?

Science is not consensus. Science is numbers.

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

hobbss wrote:
Maybe my understanding of how volatile (or maybe structs?) functions is flawed. As I understand it, the keyword volatile tells the compiler not to mess with the variable w/ regards to optimization (i.e., as the coder, I know the variable is used elsewhere, even if doesn't appear to be (say, in an ISR)). What happens if tick_happened gets optimized away, but tick doesn't? Does the struct still exist as an entity?
The C Book has an excellent page on the subject.

JJ

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

hobbss wrote:
What happens if tick_happened gets optimized away, but tick doesn't? Does the struct still exist as an entity?

If the code could tell (e.g. taking the struct's address then looking at raw memory), the compiler wouldn't be able to optimize it out.

timingStatus.tick_happened and timingStatus.tick are two separate objects. They are members of a struct, so there their addresses have a well-defined relation, but that's it. As far as behavior, they act the same as having two globals

volatile uint8_t timingStatus_tick;
uint8_t timingStatus_tick_happened;
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the clarification. Learned something today...

Science is not consensus. Science is numbers.

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

With an older GCC, I once had an conspicuity with a pointer on a struct of this kind on a high optimization level. The pointer itself was not volatile, only certain members of the target struct.
The behaviour was all right, when I defined the complete struct as volatile.

In the beginning was the Word, and the Word was with God, and the Word was God.