home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.std.c++
- Path: sparky!uunet!mcsun!news.funet.fi!network.jyu.fi!sakkinen
- From: sakkinen@jyu.fi (Markku Sakkinen)
- Subject: Re: Zero-length structures and pointer comparisons
- Message-ID: <1992Sep10.094957.23588@jyu.fi>
- Organization: University of Jyvaskyla, Finland
- References: <9225302.22791@mulga.cs.mu.OZ.AU> <4945@holden.lulea.trab.se>
- Date: Thu, 10 Sep 1992 09:49:57 GMT
- Lines: 61
-
- In article <4945@holden.lulea.trab.se> jbn@lulea.trab.se (Johan Bengtsson) writes:
- >fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON) writes:
- >: [ ... ]
- > [ ... ]
- >:I believe. For example, the following program
- >: #include <assert.h>
- >: int main() {
- >: int x,y;
- >: assert(&x != &y);
- >: }
- >: should cause undefined behavior. [Is this correct?]
- >
- >I'd say yes. On a segmented architecture (can you say 'PC'?)
- >pointer comparison need not use the full 32 bits for pointer comparison,
- >instead a 16 bit offset may be used (note that this limits arrays to 64 KB).
- > [ ... ]
- >Anyway, the current rules do seem to cause trouble for the common (?)
- >idiom for operator =:
- >
- >const T& operator = ( const T& t )
- >{
- > if ( &t == this ) return; // avoid self-assignment
- > // ...
- >}
- >
- >Is the above idiom broken?
- >Should the current rules be changed (inflicting a small performance penalty
- >on some machines)?
-
- Oh dear, why have I not noted this _horrible_ defect in the language
- definition before? Probably because it was too bad to be suspected.
- The above idiom is certainly common, and it is used in Stroustrup's
- own books. The first example I could found now in "The C++ P. L." (2. ed.)
- is in 8.3.3 (p. 266). In one member function of the list class there is
- code like this:
- slink* f = last->next;
- if (f == last)
- last = 0;
- else
- last->next = f->next;
-
- I can see no warning in the vicinity telling that the meaning of this code
- is implementation dependent or undefined!
-
- My opinion: the idiom is sensible, in many cases even necessary.
- It is the pertinent part in the language definition that is broken,
- obviously (as explained by Johan above) in order to facilitate
- efficient implementation on obsolescent machine architectures.
- Was it William Wulf who wrote approximately: "More sins in programming
- are committed in the name of efficiency than all other reasons combined,
- including sheer stupidity"?
-
- ----------------------------------------------------------------------
- Markku Sakkinen (sakkinen@jytko.jyu.fi)
- SAKKINEN@FINJYU.bitnet (alternative network address)
- Department of Computer Science and Information Systems
- University of Jyvaskyla (a's with umlauts)
- PL 35
- SF-40351 Jyvaskyla (umlauts again)
- Finland
- ----------------------------------------------------------------------
-