15

I can see practical use for a const volatile qualified variable, like

const volatile uint64_t seconds_since_1970;

if an underlying hardware mechanism updates the value every second, but the variable is not writable in the (possibly embedded) hardware. And since all three (four in C11) type qualifiers are considered independent, all combinations do seem to be allowed. But I'm at a loss imagining a real-life situation where a restrict volatile qualified pointer would really make sense:

uint32_t * restrict volatile pointer_to_some_uint32;

[EDIT: To clarify: Both volatile and restrict apply to the pointer, not to the object pointed to!]

Is this a construct allowed by the language but useless by itself, or am I missing some application area where this can be valuable?

2
  • Four? const volatile restrict ......Could you tell me what is the fourth qualifier? Sorry for my little knowledge >o<
    – ikh
    May 6, 2014 at 10:17
  • 2
    @ikh C11 defines a qualifier _Atomic. May 6, 2014 at 15:37

1 Answer 1

12

Without restrict, a non-volatile pointer could alias a volatile pointer. Thus, after every modification of an object through the volatile pointer, register-cached values of all potentially-pointer-referenced objects of the same type must be discarded.

With restrict, you can tell the compiler the volatile pointer will not alias, so that the overhead of volatile only applies to the object pointed to, and not all other objects of the same type that might be accessible via pointers.

21
  • In my example the volatile applies to the pointer, not to the object it points to. I know that using volatile uint32_t * restrict pointer; may be very useful indeed, but declaring the pointer itself both volatile and restrict doesn't move the volatile to the object pointed to, does it? Jan 24, 2012 at 15:40
  • 1
    @Johan: Consider that perhaps an underlying hardware mechanism updates pointer_to_some_uint32 from time to time (which is far-fetched, but an implementation could do it), and that furthermore the address it writes there is guaranteed not to be aliased by any other pointer. Then it's volatile restrict. Normally though, restrict is good for function arguments and volatile is pointless for local variables, so you've picked an example that's unlikely to be used even though it's not strictly useless. Jan 24, 2012 at 15:51
  • @SteveJessop Yes, that's my interpretation as well. The only thing I've come up with is some kind of garbage collector rearranging memory and running in a different thread/process context. Such pointers could validly be both volatile (since the GC might change the pointers) and restrict if no other pointers point to the GC-admin sections of the memory chunks maintained by the GC. But the answer from @R.. seems to indicate the object pointed to "inherits" the volatile ("the overhead of volatile only applies to the object pointed to"), which is incorrect IMHO. Jan 24, 2012 at 16:03
  • @Johan: I suspect he just misread the second example in the question -- I did on the first attempt, and it wasn't until I saw your first comment above that I realised what you're asking. Jan 24, 2012 at 16:08
  • Yes, I misread it. In this case the volatile and restrict really have no interaction, but they're each useful the same ways they would always be useful... Jan 24, 2012 at 16:10

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.