Path: utzoo!attcan!uunet!husc6!spdcc!ima!haddock!karl
From: karl@haddock.ISC.COM (Karl Heuer)
Newsgroups: comp.lang.c++
Subject: Re: classes with no data members
Keywords: sizeof,empty classes,new
Message-ID: <4614@haddock.ISC.COM>
Date: 17 Jun 88 23:45:24 GMT
References: <464@polari.UUCP> <7943@alice.UUCP> <470@polari.UUCP> <10399@sol.ARPA> <472@polari.UUCP> <18223@cornell.UUCP>
Reply-To: karl@haddock.ima.isc.com (Karl Heuer)
Organization: Interactive Systems, Boston
Lines: 23

Assume the existence of a C++ compiler that allows zero-sized objects (ZSOs),
i.e. one that does not insert a shim in an empty class.

Several people have stated the opinion that any two object pointers should be
distinguishable, and that calling `new empty' twice should yield unequal
(hence non-NULL) values.  I'm not convinced.

Given `int a[N]', &a[N] is a valid pointer that doesn't point anywhere.  That
is, it's guaranteed that &a[N] can be stored in a pointer variable, but
dereferencing it is undefined.  And it's not guaranteed to be distinct from
other pointers: &a[N] may well be equal to &b[0].  I think ZSO pointers are in
the same category.  (After all, it's just the limiting case N=0.)

Also, consider `empty x[2]'.  Surely &x[0] and &x[1] must compare equal?  If
so, why should the situation be different if I declare `empty x0, x1'?  Or if
I call `new empty' twice?

Btw, in ANSI C, malloc(0) either gives you a ZSO or a NULL pointer.  In C++,
new(0) apparently hasn't been well-defined.  (This is a serious flaw, in view
of the legality of user-defined new.  The language definition should either
specify the behavior, or explicitly state that it's undefined.)

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint