Article View: comp.std.c
Article #1179Re: Zero-length structures and pointer comparisons
From: 09-Sep-1992 1034
Date: Wed, 09 Sep 1992 01:35
Date: Wed, 09 Sep 1992 01:35
31 lines
1834 bytes
1834 bytes
In article <9225302.22791@mulga.cs.mu.OZ.AU> fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON) writes:
>First, a question for comp.std.c:
>is the following a legal (strictly conforming) program?
> struct S { unsigned:0; }
> int main() { return sizeof(S); }
No. ANSI C section 3.5.2.1, page 61 lines 25 to 26: "If the
struct-declaration-list contains no named members, the behavior is undefined."
>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?]
No. For relational operators (<, >, <=, >=) that would be correct.
For equality operators (==, !=) the result is defined. Comparing whether
a pointer is equal to a null pointer or not is a common operation, and
(unless miscoded in some other way) is defined.
I don't have modern documents on C++ and don't dare to answer your later
questions. For the same reason, this answer is not cross-posted.
--
Norman Diamond diamond@jit081.enet.dec.com
If this were the company's opinion, I wouldn't be allowed to post it.
"Yeah -- bad wiring. That was probably it. Very bad."
Technical Details
Message-ID:
<9209090135.AA00853@enet-gw.pa.dec.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!olivea!spool.mu.edu!wupost!usc!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!ucbvax!jit081.enet.dec.com!diamondShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!olivea!spool.mu.edu!wupost!usc!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!ucbvax!jit081.enet.dec.com!diamond From: diamond@jit081.enet.dec.com (09-Sep-1992 1034) Newsgroups: comp.std.c Subject: Re: Zero-length structures and pointer comparisons Message-ID: <9209090135.AA00853@enet-gw.pa.dec.com> Date: 9 Sep 92 01:35:05 GMT Sender: daemon@ucbvax.BERKELEY.EDU Lines: 31