Topic: Are Null struct (class) Types Allowed?


Author: bill@twwells.com (T. William Wells)
Date: 5 Aug 93 07:37:46 GMT
Raw View
In article <23jnnk$pn6@news.cs.tu-berlin.de> jutta@cs.tu-berlin.de (Jutta Degener) writes:
: davisonj@bnr.ca (John M Davison) writes:
: >         As far as structures go, ANSI X3.159-1989, Section 3.5.2.1, Lines
: > 25-26, says that "If the struct-declaration-list contains no named members,
         ^^^^^
: > the behavior is undefined."
:
: In addition, the definition of a structure type in ANSI C 3.1.2.5
: explicitly says "nonempty"

What that is talking about is struct foo { int :1; }, which is
undefined because there are no named members in it.




Author: davisonj@bnr.ca (John M Davison)
Date: 2 Aug 1993 16:21:28 GMT
Raw View
        I hear lots of talk about creating zero-sized objects in C and C++, but
I am having some difficulty finding out exactly what the rules are for handling
objects in each language.  Margaret A. Ellis' and Bjarne Stroustrup's
_The_Annotated_C++_Reference_Manual_, Section 5.3.3, says that "...an operator
new() can be called with the argument zero."  ANSI X3.159-1989, Section
4.10.3.3, doesn't explicitly say what happens if zero is passed to a malloc()
call.

        As far as structures go, ANSI X3.159-1989, Section 3.5.2.1, Lines
25-26, says that "If the struct-declaration-list contains no named members, the
behavior is undefined."  Does this mean that the following C/C++ program
produces undefined behavior?  (Is it legal C?  C++?)

          struct a{};main(){struct a b;return 0;}

        Is there a place in ANSI X3.159-1989,
_The_Annotated_C++_Reference_Manual_, or any other easily obtainable piece of
literature that explicitly talks about this aspect of C/C++ structures?

--
John Davison, davisonj@ecn.purdue.edu <---- send followups to THIS ADDRESS!!!!!
(Otherwise your mail will bounce!)  If you work at NT/BNR, please COCOS to
"John Davison".  The information contained in this article isn't necessarily
representative of Northern Telecom or BNR.




Author: jutta@cs.tu-berlin.de (Jutta Degener)
Date: 2 Aug 1993 18:51:32 GMT
Raw View
davisonj@bnr.ca (John M Davison) writes:
>         As far as structures go, ANSI X3.159-1989, Section 3.5.2.1, Lines
> 25-26, says that "If the struct-declaration-list contains no named members,
> the behavior is undefined."

In addition, the definition of a structure type in ANSI C 3.1.2.5
explicitly says "nonempty"

| * A /structure type/ describes a sequentially allocated nonempty
|   set of member objects, each of which has an optionally
|   specified name and possibly distinct type.

and the grammar for a structure declaration in ANSI C 3.5.2.1 does not
allow the struct-declaration-list to remain empty, either

|       struct-or-union-specifier:
|               struct-or-union identifier[opt] { struct-declaration-list }
|               struct-or-union identifier
[...]
|       struct-declaration-list:
|               struct-declaration
|               struct-declaration-list struct-declaration
|
|       struct-declaration:
|               specifier-qualifier-list struct-declarator-list ;

moving us from undefined behavior to a violation of a syntax rule,
which requires a diagnostic message.

Jutta Degener (jutta@cs.tu-berlin.de)




Author: diamond@jit.dec.com (Norman Diamond)
Date: 3 Aug 1993 01:14:00 GMT
Raw View
In article <23jeu8$au3@crchh327.bnr.ca> jackw@bnr.ca (John Davison in care of Jack Wasserman) writes:
>        I hear lots of talk about creating zero-sized objects in C and C++,
[...]
>        As far as structures go, ANSI X3.159-1989, Section 3.5.2.1, Lines
>25-26, says that "If the struct-declaration-list contains no named members,
>the behavior is undefined."  Does this mean that the following C/C++ program
>produces undefined behavior?  (Is it legal C?  C++?)
>          struct a{};main(){struct a b;return 0;}

In addition to your quotation of an answer for C, Jutta Degener posted a
correct answer (of course) for C, but didn't answer about C++.

Ellis & Stroustrup, section 9, page 163 last line, says that a member-list
between the braces is optional.  And page 164 near the middle, "Objects of
an empty class have nonzero size."

(There is no contradiction.  Most valid C++ programs are not valid C programs.)
--
 <<  If this were the company's opinion, I would not be allowed to post it.  >>
A program in conformance will not tend to stay in conformance, because even if
it doesn't change, the standard will.       Force = program size * destruction.
Every technical corrigendum is met by an equally troublesome new defect report.