home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.std.c:2589 comp.std.c++:1134
- Newsgroups: comp.std.c,comp.std.c++
- Path: sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
- From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
- Subject: Zero-length structures and pointer comparisons
- Message-ID: <9225302.22791@mulga.cs.mu.OZ.AU>
- Sender: news@cs.mu.OZ.AU
- Organization: Computer Science, University of Melbourne, Australia
- Date: Tue, 8 Sep 1992 16:14:16 GMT
- Lines: 41
-
- First, a question for comp.std.c:
- is the following a legal (strictly conforming) program?
-
- struct S { unsigned:0; }
- int main() { return sizeof(S); }
-
- Secondly, some comments about C++.
- In ANSI C, pointer comparisons between pointers to unrelated objects (ie.
- objects that are not both members of the same aggregate) cause undefined
- behavior, 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?]
-
- Now the C++ reference manual (5.9) says basically the same thing, with the
- further constraints that comparisons with pointers to static members are
- undefined, as are comparisons between pointers to members whose declarations
- are separated by an access-class specifier (public/protected/private).
-
- Section 5.9 says that "Two pointers to the same object compare equal",
- but it does NOT say that "pointers to distinct objects compare unequal" -
- instead it says "... Other pointer comparisons are implementation dependent."
-
- Given that this is the case, it seems contradictory that repeated calls to
- operator new(0) are required to return pointers to "distinct objects".
- How could a conforming program possibly detect whether this requirement
- was actually met? Surely the following program
- #include <assert.h>
- int main() {
- assert(operator new(0) != operator new(0));
- }
- is not strictly conforming, is it?
-
- --
- Fergus Henderson fjh@munta.cs.mu.OZ.AU
- This .signature virus is a self-referential statement that is true - but
- you will only be able to consistently believe it if you copy it to your own
- .signature file!
-