Path: csiph.com!weretis.net!feeder6.news.weretis.net!news.misty.com!news.iecc.com!.POSTED.news.iecc.com!nerds-end From: dave_thompson_2@comcast.net Newsgroups: comp.compilers Subject: Re: Undefined Behavior Optimizations in C Date: Sat, 28 Jan 2023 10:35:18 -0500 Organization: A noiseless patient Spider Sender: johnl@iecc.com Approved: comp.compilers@iecc.com Message-ID: <23-01-076@comp.compilers> References: <23-01-009@comp.compilers> <23-01-011@comp.compilers> <23-01-012@comp.compilers> <23-01-017@comp.compilers> <23-01-027@comp.compilers> <23-01-032@comp.compilers> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 8bit Injection-Info: gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="12030"; mail-complaints-to="abuse@iecc.com" Keywords: C, Fortran, history Posted-Date: 29 Jan 2023 11:56:51 EST X-submission-address: compilers@iecc.com X-moderator-address: compilers-request@iecc.com X-FAQ-and-archives: http://compilers.iecc.com Xref: csiph.com comp.compilers:3344 On Tue, 10 Jan 2023 17:32:28 +0100, David Brown wrote: [ UB example: generated code assumes bool parameter is only 0 or 1 and mishandles anything else, but misdeclared call passes 2 ] > This is clearly wrong - clearly undefined behaviour. And the result > would be a formatted disk. But there is nothing wrong with the > compiler's generated code. I have seen other occasions when compiler's > have made code with booleans that appear to be both true and false, or > neither true nor false, as a result of undefined behaviour setting the > underlying memory to something other than 0 or 1, simply because that > was the result of the most efficient code. > FWIW -- back in the 80s the VAX FORTRAN compiler checked only the _low bit_ of a LOGICAL variable (stored as a byte or a 4-byte word, I forget which) because this was faster. People often reused (scarce and expensive) memory then and such a variable might accidentally get set to a value other than 0 or 1, producing surprising/confusing results. In those days DEC was vigorously opposing C (they had BLISS instead) and Unix, so there was no DEC C compiler, though I'm pretty sure there was a DECUS (user group) one, and of course there was not yet a C standard at all much less one including bool.