const, volatile, etc [was Re: #defines with parameters]
Piercarlo Grandi
pcg at aber-cs.UUCP
Sat Dec 10 04:52:16 AEST 1988
In article <1450 at micomvax.UUCP> ray at micomvax.UUCP (Ray Dunn) writes:
In article <10919 at ulysses.homer.nj.att.com> ggs at ulysses.homer.nj.att.com (Griff Smith) writes:
>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).
They were not forbidden, you could do any optimization that did not impact
the semantics of programs. A Classic C program compiled with a dpANS C
compiler changes semantics, unless you tag with volatile all variables not
tagged register.
There is the implication here that the introduction of the "volatile"
qualifier is the *cause* of the problem.
This is of course not true, the qualifier is an attempt to provide a
*solution* to the problem.
It IS the cause! volatile is not necessary, register is sufficient...
I have tried to give arguments for this (at length :->).
In pre-ANSI 'C', *all* variables are regarded as being *non-volatile*,
the compiler is free for example to keep the variable in a register, and
there is usually no way to selectively stop the compiler optimizing their
references away.
Exactly the opposite! In Classic C the concept was that the compiler would
take register as an hint of which variables could always be safely cached in
register (and the user be forbidden to take pointer to them), and all the
others could be cached only if it was guaranteed that doing so would not
"cause surprises".
You usually can turn off optimizations globally while compiling a
module. One technique to use is to collect all fucntions containing
volatile stuff into one or more modules, and only compile *them* with
optimisation off.
Think again. You REALLY want the semantics of your code dependent on the
flags you give to a compiler?
The "volatile" qualifier is a *much* better solution when you have an
ANSI Conforming compiler.
Actually it is a backwards step wrt to register, as it encourages bloated
compilers and the delusion that they can select which variables to put in
registers better than you can do, while requiring careful analysis as to
which variables MUST be declared volatile (as failing to tag them makes
your program erroneous, where register is perfectly safe).
--
Piercarlo "Peter" Grandi INET: pcg at cs.aber.ac.uk
Sw.Eng. Group, Dept. of Computer Science UUCP: ...!mcvax!ukc!aber-cs!pcg
UCW, Penglais, Aberystwyth, WALES SY23 3BZ (UK)
More information about the Comp.lang.c
mailing list