GCC warnings for unused global variables

Paul Koning pkoning@equallogic.com
Fri May 2 15:27:00 GMT 2003


>>>>> "Gabriel" == Gabriel Dos Reis <gdr@integrable-solutions.net> writes:

 Gabriel> Paul Koning <pkoning@equallogic.com> writes:
 >> I can't tell if you're trying to say something that goes directly
 >> against the intent of volatile or not, so let's talk about an
 >> example:

 Gabriel> I'm trying to say that "volatile" is not a *reliable*
 Gabriel> feature to use to write robust code with reproducible
 Gabriel> behaviour.

I don't understand.

"volatile" exists in order to be able to write device drivers and
multiprocessor applications that are robust and have reproducible
behavior.  That's how it has always been used; that's why it exists.

Now you seem to be saying that such code is just lucky when it works.
Why?

 >> static volatile int foo;
 >> 
 >> void test (void) { foo = 1; foo = 1; }
 >> 
 >> Does your statement mean that you believe the compiler is allowed
 >> to optimize this into: void test (void) { foo = 1; } ?

 Gabriel> If it can determine that does nto change the behaviour of
 Gabriel> whole program, yes, it is permitted to do so.

That doesn't really help.  Do you think that the compiler IS able to
determine this and therefore make that change?

Fergus and others pointed out that the compiler cannot possibly make
that determination because by definition volatile variables are
allowed to have side effects that the compiler is unaware of.  

So if what you mean is "yes, if the compiler could tell, it would be
allowed to, but the compiler can by definition never tell, so it
actually is not allowed to" then we are in agreement.

On the other hand, if you mean "it doesn't currently but at some point
in the future gcc will start doing this because it thinks it can make
that determination" then at that point every OS and SMP application
will break.

     paul



More information about the Gcc mailing list