Path: utzoo!attcan!uunet!mcvax!ukc!dcl-cs!aber-cs!pcg From: pcg@aber-cs.UUCP (Piercarlo Grandi) Newsgroups: comp.lang.c Subject: Re: const, volatile, etc [was Re: #defines with parameters] Summary: Classic C: everything volatile except register Keywords: volatile register optimization Message-ID: <369@aber-cs.UUCP> Date: 9 Dec 88 18:52:16 GMT References: <674@quintus.UUCP> <117@halcdc.UUCP> <468@auspex.UUCP> <9016@smoke.BRL.MIL> <10919@ulysses.homer.nj.att.com> <1450@micomvax.UUCP> Reply-To: pcg@cs.aber.ac.uk (Piercarlo Grandi) Distribution: eunet,world Organization: CS Dept., University College of Wales, Aberystwyth, UK (Disclaimer: my statements are purely personal) Lines: 54 In article <1450@micomvax.UUCP> ray@micomvax.UUCP (Ray Dunn) writes: In article <10919@ulysses.homer.nj.att.com> ggs@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@cs.aber.ac.uk Sw.Eng. Group, Dept. of Computer Science UUCP: ...!mcvax!ukc!aber-cs!pcg UCW, Penglais, Aberystwyth, WALES SY23 3BZ (UK)