const, volatile, etc [was Re: #defines with parameters]

Henry Spencer henry at utzoo.uucp
Sat Dec 3 05:57:56 AEST 1988


In article <10919 at ulysses.homer.nj.att.com> ggs at ulysses.homer.nj.att.com (Griff Smith) writes:
>> "volatile" also serves a need...
>
>I agree, and I don't object, but some brief encounters with an ANSI
>compiler make me worry about it a bit...

Well, if I was to quibble, I would observe that there is no such thing
as an ANSI C compiler yet, since there is no ANSI standard yet.  In fact,
any compiler that is actually in users' hands right now probably does not
fully match even the current draft.  End of quibble...

>The problem is that the
>existence of the volatile qualifier makes it possible to do
>optimizations that were previously forbidden (or at least difficult to
>slip past the customers).

No, actually, it makes it possible for the users to defend themselves
against optimizations that the compiler writers had in the works anyway.
Even in the Good Old Days it wasn't uncommon to find machines where it
was common knowledge that one should not compile tricky code (e.g. the
kernel) with -O.

>I blew several hours discovering that a flag
>set by a signal handler had been optimized out of existence because it
>wasn't declared volatile.  If I were writing new software I would be
>aware of the problem and use the proper declaration, but what am I to
>do about fixing all the old stuff that now has subtle errors caused by
>optimizations that used to be illegal?

The old stuff always had subtle errors; the standard did not create them,
it simply called them to your attention.  The behavior of signal handlers
has never been guaranteed in any way.  Don't confuse "what I can get away
with on a 4.2BSD VAX" with the definition of C.  Those optimizations were
never illegal, they just weren't common.  The standard has actually
improved the situation -- at least now you have behavior that you can
count on.

The only way to fix the old stuff is, well, to fix it.  You can use a
"#define volatile /* */" to make it compatible with old compilers.
-- 
SunOSish, adj:  requiring      |     Henry Spencer at U of Toronto Zoology
32-bit bug numbers.            | uunet!attcan!utzoo!henry henry at zoo.toronto.edu



More information about the Comp.lang.c mailing list