tags:

views:

846

answers:

3

According to me, it is zero but there seems to be bit confusion here

I have tested it with gcc compiler and it gives me zero as output. I know that in C++, size of an empty class is 1. Let me know if I am missing anything here.

+26  A: 

A struct cannot be empty in C because the syntax forbids it. Furthermore, there is a semantic constraint that makes behavior undefined if a struct has no named member:

struct-or-union-specifier:
  struct-or-union identifieropt { struct-declaration-list }
  struct-or-union identifier

struct-or-union:
  struct
  union

struct-declaration-list:
  struct-declaration
  struct-declaration-list struct-declaration

struct-declaration:
  specifier-qualifier-list struct-declarator-list ;

/* type-specifier or qualifier required here! */
specifier-qualifier-list:
  type-specifier specifier-qualifier-listopt
  type-qualifier specifier-qualifier-listopt

struct-declarator-list:
  struct-declarator
  struct-declarator-list , struct-declarator

struct-declarator:
  declarator
  declaratoropt : constant-expression

If you write

struct identifier { };

It will give you a diagnostic message, because you violate syntactic rules. If you write

struct identifier { int : 0; };

Then you have a non-empty struct with no named members, thus making behavior undefined, and not requiring a diagnostic:

If the struct-declaration-list contains no named members, the behavior is undefined.

Notice that the following is disallowed because a flexible array member cannot be the first member:

struct identifier { type ident[]; };
Johannes Schaub - litb
but following code works perfectly with gcc compiler and it prints zero ` struct abc { }; printf("size of empty struct %d\n", sizeof(struct abc));'
vinit dhatrak
compiling with `-ansi -pedantic` gives "main.c:2: warning: struct has no members"
Johannes Schaub - litb
Empty structs are a GCC extension.
Michael Burr
@litb gr8 input !!!
vinit dhatrak
I don't believe the C standard says a diagnostic is required if you violate a rule contained only in the BNF. For the most part, a diagnostic is required only if a "shall" or "shall not", contained with a "constraints" clause, is violated. In C++, the situation is slightly different (it specifies that "The set of *diagnosable rules* consists of all syntactic and semantic rules in this International Standard..."
Jerry Coffin
C99 at least says "A conforming implementation shall produce at least one diagnostic message (identified in an implementation-defined manner) if a preprocessing translation unit or translation unit contains a violation of any syntax rule or constraint, ..."
Johannes Schaub - litb
Congrats on maxing your rep out on one answer... :P
GMan
"With the gcc compiler" being the operative words; this is a gcc-specific extension (of questionable value IMO). Visual Studio rightly chokes on it.
John Bode
Oh, thanks so much. I appreciate that you enjoy the rep festival with me.
Johannes Schaub - litb
+3  A: 

In C99: "If the struct-declaration-list contains no named members, the behavior is undefined."

The syntax doesn't really allow it anyway, though I don't see anything that says a diagnostic is required, which puts it pretty much back in the "undefined behavior" camp.

Jerry Coffin
"No named members" refers to the specific situation with unnamed bitfleld (see litb's reply above). A plain empty struct (no members at all) is a constraint violation, not UB.
AndreyT
I've just reread it, and I see no such constraint. I'm looking at §6.7.2.1. The constraints I see are in paragraphs 2 through 4. Paragraph 2 says the only allowed incomplete type is a flexible array. Paragraph 3 says a bitfield can have a width of zero and no name, or a name and a width between 1 and the size of the underlying object. Paragraph 4 says the underlying type of a bitfield shall be _Bool, unsigned, or int (or something else that's implementation defined).
Jerry Coffin
It's a syntax rule violation, which demands a diagnostic message.
Johannes Schaub - litb
If it's a syntax rule violation, then it certainly requires a diagnostic -- but I can't see anything that says the BNF forms part of the syntax rules. For the most part, they're treated as non-normative (e.g. Appendix A contains the whole BNF, but is specifically labeled as "informative").
Jerry Coffin
@Jerry, the rules within "Syntax" sections (like 6.7.2.1/1 in C99) are syntax rules of course. Annex A only summarizes all rules that are given already in clause 6, thus it's informative.
Johannes Schaub - litb
"Of course" usually translates to having taken something for granted without any real support -- and this seems to be no exception. Don't get me wrong -- I'm not saying you're wrong (exactly), only that this is the sort of thing it should explicitly specify, not take for granted.
Jerry Coffin
+2  A: 

The C grammar doesn't allow the contents of a struct to be empty - there has to be at least an unnamed bitfield or a named member (as far as the grammar is concerned - I'm not sure if a struct that contains only an unnamed bitfield is otherwise valid).

Support for empty structs in C are an extension in GCC.

In C++ and empty struct/class member-specification is explicitly permitted, but the size is defined to be 1 - unless as part of the empty base optimization the compiler is allowed to make an empty base class take no space in the derived class.

Michael Burr