Newsgroups: comp.lang.c Path: utzoo!henry From: henry@utzoo.uucp (Henry Spencer) Subject: Re: const, volatile, etc [was Re: #defines with parameters] Message-ID: <1988Dec2.195756.9092@utzoo.uucp> Organization: U of Toronto Zoology References: <674@quintus.UUCP> <117@halcdc.UUCP> <468@auspex.UUCP> <9016@smoke.BRL.MIL> <10919@ulysses.homer.nj.att.com> Date: Fri, 2 Dec 88 19:57:56 GMT In article <10919@ulysses.homer.nj.att.com> ggs@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@zoo.toronto.edu