Path: utzoo!utgpu!water!watmath!clyde!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: <4779@haddock.ISC.COM> Date: 27 Jun 88 23:48:57 GMT References: <464@polari.UUCP> <7943@alice.UUCP> <470@polari.UUCP> <10399@sol.ARPA> <4614@haddock.ISC.COM> <475@polari.UUCP> Reply-To: karl@haddock.ima.isc.com (Karl Heuer) Organization: Interactive Systems, Boston Lines: 50 In article <475@polari.UUCP> rlb@polari.UUCP (rlb) writes: >In article <4614@haddock.ISC.COM>, karl@haddock.ISC.COM (Karl Heuer) writes: >>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.) > >The Walking Lint stumbles a bit here :-). It certainly is guaranteed to be >distinct from a possibly quite large number of other pointers -- those that >point to a[0] through a[N-1]. You're misparsing me. I'm not saying that it compares unequal to everything; I'm saying that &a[N] has a `flaw' which valid pointers do not: &a[N] might compare equal to &b[0]. >It is true that a pointer to &a[N] cannot be dereferenced legally and you may >personally choose to define that as "doesn't point anywhere" but: A) that >definition does not remove the restrictions on the pointer (must play well >with other pointers into the same object :-), I'm not sure I understand why we're concerned with other pointers into the same object. I thought the question was whether &x == &y should be forbidden for two distinct ZSOs. >and B) this is all just an analogy (not quite a correct one, I think) for the >subject under discussion, therefore we proceed... As I said, take the limiting case N=0. An array of size zero is one type of ZSO; I would expect its properties to be similar to both an empty class and a nonempty array. >>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? > >... To use your own example, &x[0] and &x[1] definitely must NOT compare >equal, because the expression "&x[1] - &x[0]" must evaluate to the integer 1. &x[1] is x+1, which is (empty *)((char *)x + sizeof(empty)), which is &x[0]. &x[1]-&x[0] is the difference in bytes divided by the size of the type, which is 0/0 (indeterminate). Looks like ZSO-pointer subtraction is undefined. >Else you must clutter the language definition up with a great many "except >for zero-sized objects". I don't think this can be avoided -- except by not implementing ZSOs, which is of course what we see in practice. For good reasons, it seems. Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint