5

I think I have a tricky question, but I'm sure you will be able to help me. Let's say I have a function like this:

char my_function (int example);

I use this function in multiple cases, sometimes the argument it receives is a volatile variable and sometimes a non-volatile variable. That cause some warnings when I compile my code that can be easily removed by using casts, but I want to understand which is the safer scenario and why.

Scenario 1:

Prototype: char my_function (int example);

int a;
volatile int b;

my_function (a);  // Everything is fine.
my_function ((int)b);  // Avoided the warning, by casting the variable and            saying it's no longer volatile. 

Scenario 2:

Prototype: char my_function (volatile int example);

int a;
volatile int b;

my_function(b);  // Everything is fine.
my_function((volatile int)a); // Avoided the warning, by casting 'a' saying that now it's volatile.

I understand how volatile modifier works, I mostly use it because I program micro-controllers and I need to ensure that some of my variables are never optimized out when they are hardware modified. I am a bit confused about casting the volatile modifier and that is why I want to understand which is the safer scenario apart from just removing the warning.

20
  • 1
    Possible duplicate of volatile variables as argument to function
    – kdopen
    Aug 16, 2016 at 17:45
  • 1
    volatile is not a modifier, but a qualifier. There are no modifiers in C. And your question is not clear. Although you state different, your text lets suspect you do not know how volatile works. Revisit your C book or get a better one. And never cast an expression until you are aware of all implications. I have strong doubts. Aug 16, 2016 at 17:47
  • 2
    Using volatile in conjunction with pointer parameters can make sense; if the data the pointer points at can change behind the scenes, that's crucial for the compiler to know. But simple value parameters marked volatile; that's tantamount to an abuse of volatile. Aug 16, 2016 at 17:51
  • 1
    @kdopen This seems to be the opposite of that question, which is about having the volatile qualifier on the function argument/return type, not the variable being passed to the function.
    – Barmar
    Aug 16, 2016 at 17:51
  • 1
    @cventu English is not Olaf's native language either, though it is hard to tell. Aug 16, 2016 at 17:55

2 Answers 2

6

It really depends on what my_function does with its argument.

Remember that volatile prevents certain optimizations - predominantly it forces the variable to be re-read every time it is referenced. Thus this code

volatile int a;
int b;
// ...
b = a + 1;
b = a + 2;

will read a for each statement and, as a may have changed values between them, give the correct result.

When you pass a volatile into a function as a parameter, you only get one read of the variable. This may then be used multiple times within the function (effectively losing the volatile nature).

Remember that C is pass-by-value. When you invoke the function as

my_function((int)b); // b is declared volatile

The compiler generates code to read b once in the calling code, and push the value it read onto the stack (usually), then invoke my_function. This copy is then referenced within my_function as example, and no matter how often you reference example you will always get the same value (even if the original b variable has since changed many times).

That might be exactly what you want - take a snapshot of the variable and do several computations on its value.

If it's not what you want, you need to consider passing in a pointer with the appropriate volatile qualifications.

char my_function( volatile int *example);

And call it thus:

my_function(&a);
my_function(&b);

Then reference *example inside my_function.

9
  • 2
    That is pretty nonsense. If the variable you pass into a function shall be volatile, you have to declare the variable volatile, not cast the _expression you use for the parameter to volatile. I can'tr even see how that would change anything in the code, because you cast the expression to volatile, then assign the result to a non-volatile parameter. Aug 16, 2016 at 18:54
  • 1
    Your pointer example, though valid usage, is completely different, as it does not qualify the parameter volatile, but the object it points to. A volatile pointer would be int * volatile p and of as little use as the int i parameter. Aug 16, 2016 at 18:57
  • And it is indeed the "value it points to" which is volatile, not the pointer.
    – kdopen
    Aug 16, 2016 at 18:58
  • 2
    values cannot be volatile (that is the problem with the cast), but objects can. Btw., there is one application to use volatile aguments, but this is not used here and I strongly discourage to use it. Aug 16, 2016 at 18:59
  • 1
    "The compiler generates code to read b once in the calling code, and push the value it read onto the stack (usually)" - There is no requirement to actually read b, nor is there any requirement for a stack in C. Even if you refer to typical implementations ("usually"), this is typically wrong for such functions, are they typically pass the first (and often subsequent) arguments in registers (accumulator, GP registers, etc). Aug 16, 2016 at 19:07
1

The cast doesn't actually do anything. In the call my_function (b); the code reads the volatile int b. That's where the "volatile" matters, during the read. The result of the read is already an int and not a volatile int. There are no volatile int values. Even if there were volatile int values, passing it to my_function would convert it to plain int, just as the cast does.

It may be that the compiler assumes that passing a volatile int variable to a function is something dangerous worth a warning, and by adding a cast to int you indicate that you know what you are doing.

1
  • That warning would definitively wrong. The volatile qualifier is not part of the value, but only lvalues (like all qualifiers, see 6.7.3p4). Such a compiler is suspectible to have other faults. I'm not sure I'd be able to trust that thing to generate proper code. Aug 16, 2016 at 19:25

Your Answer

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.