🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #1488

Re: Incrementing a bool

#1488
From: Richard A. O'Keefe
Date: Thu, 03 Aug 1995 04:05
34 lines
1771 bytes
>|> >As someone else pointed out, bool++ is a nice shorthand for test-and-set.

I am surprised that no-one else has pointed out that test-and-set is
*WRONG* for initialisation.  This should be another nail in the coffin
of bool++.  Let me explain.

In most cases, the right way to code one-time initialisation is

	{
	    static int initialised = 0;

	    if (!initialised) {
		/* many lines of initialisation code */
		initialised = 1;
	    }
	    ...
	}

The 'initialised' flag should mean "initialisation has been COMPLETED".
If you set the flag too early, as in

	    if (!initialised) {
		initialised = 1;
		/* many lines of initialisation code */

then you _may_ never be sure that initialisation has been done.   Why?
Think about signals, signal handlers, longjmp(), and so on.

I learned about this problem only a year after I started coding C.

-- 
"conventional orthography is ... a near optimal system for the
 lexical representation of English words." Chomsky & Halle, S.P.E.
Richard A. O'Keefe; http://www.cs.rmit.edu.au/~ok; RMIT Comp.Sci.
Technical Details
Message-ID: <3vphuu$evs@goanna.cs.rmit.edu.au>
Path: arc1-ca.pugleaf.net!goanna.cs.rmit.edu.au!not-for-mail
References: <3u1113$pjs@fido.asd.sgi.com> <DBx29s.HKo@research.att.com> <3uh5dg$cku@silver.jba.co.uk> <KANZE.95Jul19114530@slsvhdt.lts.sel.alcatel.de> <3uukvi$t6s@metro.ucc.su.OZ.AU> <u9enzflhkl.fsf@phydeaux.cygnus.com> <3v65hv$64o@eco.twg.com> <3va8el$id@gabi.gabi-soft.fr>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!goanna.cs.rmit.edu.au!not-for-mail
From: ok@goanna.cs.rmit.edu.au (Richard A. O'Keefe)
Newsgroups: comp.std.c++,comp.std.c
Subject: Re: Incrementing a bool
Date: 3 Aug 1995 14:05:50 +1000
Organization: Comp Sci, RMIT, Melbourne, Australia
Message-ID: <3vphuu$evs@goanna.cs.rmit.edu.au>
References: <3u1113$pjs@fido.asd.sgi.com> <DBx29s.HKo@research.att.com> <3uh5dg$cku@silver.jba.co.uk> <KANZE.95Jul19114530@slsvhdt.lts.sel.alcatel.de> <3uukvi$t6s@metro.ucc.su.OZ.AU> <u9enzflhkl.fsf@phydeaux.cygnus.com> <3v65hv$64o@eco.twg.com> <3va8el$id@gabi.gabi-soft.fr>
NNTP-Posting-Host: goanna.cs.rmit.edu.au
NNTP-Posting-User: ok
X-Newsreader: NN version 6.5.0 #0 (NOV)