In C, how do you declare the members of a structure as volatile?
Asked Answered
U

4

23

How do you declare a particular member of a struct as volatile?

Unstressed answered 11/6, 2009 at 6:52 Comment(2)
Just a warning on the C/C++ volatile keyword. Unless you know what you are doing you should never use it. C/C++ volatile != java/C# volatile volatile does not help in threaded code unless you really know what you are doing, you need to use C++0x atomic template (or something similar). The only time I've had to use volatile is when controlling hardware registers.Offcolor
Shane, the same could be said for while loops or, indeed, anything. Very little will help in threaded/any code unless you know what you're doing. The idea is that you learn how to use it :-)Stricture
S
38

Exactly the same as non-struct fields:

#include <stdio.h>
int main (int c, char *v[]) {
    struct _a {
        int a1;
        volatile int a2;
        int a3;
    } a;
    a.a1 = 1;
    a.a2 = 2;
    a.a3 = 3;
    return 0;
}

You can mark the entire struct as volatile by using "volatile struct _a {...}" but the method above is for individual fields.

Stricture answered 11/6, 2009 at 6:59 Comment(0)
C
12

Should be pretty straight forward according to this article:

Finally, if you apply volatile to a struct or union, the entire contents of the struct/union are volatile. If you don't want this behavior, you can apply the volatile qualifier to the individual members of the struct/union.

Ceresin answered 11/6, 2009 at 6:59 Comment(1)
The page is not available now.Diamond
I
10

I need to clarify volatile for C/C++ because there was a wrong answer here. I've been programming microcontroleurs since 1994 where this keyword is very useful and needed often.

volatile will never break your code, it is never risky to use it. The keyword will basically make sure the variable is not optimized by the compiler. The worst that shold happen if you overuse this keyword is that your program will be a bit bigger and slower.

Here is when you NEED this keyword for a variable : - You have a variable that is written to inside an interrupt function. AND - This same variable is read or written to outside interrupt functions. OR If you have 2 interrupt functions of different priority that use the variable, then you should also use 'volatile'.

Otherwise, the keyword is not needed.

As for hardware registers, they should be treated as volatile even without the keyword if you don't do weird stuff in your program.

Iloilo answered 15/5, 2018 at 15:35 Comment(0)
W
-2

I just finished a data structure in which it was obvious where the volatile qualifier was required, but for a different reason than the ones stated above: It is simply because the struct requires a forceful locking mechanism because of (i) direct access and (ii) equivalent invocation.


Direct access deals with sustained RAM reading and writing.

Equivalent invocation deals with interchangeable method flows.


I haven't had much luck with this keyword unless the compiler knows exactly what to do about it. And that's my own personal experience. But I am interested in studying how it directly impacts a cross-platform compilation such as between a low-level system call and a back-end database.

Wolves answered 27/8, 2019 at 16:42 Comment(1)
For future reference as to why people might be voting this down: this answer does not answer the question that was asked.Transom

© 2022 - 2024 — McMap. All rights reserved.