26

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.

Community
  • 1
  • 1
Vinit Dhatrak
  • 6,814
  • 8
  • 27
  • 30

4 Answers4

44

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
  • 496,577
  • 130
  • 894
  • 1,212
  • 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 Oct 26 '09 at 18:35
  • 10
    compiling with `-ansi -pedantic` gives "main.c:2: warning: struct has no members" – Johannes Schaub - litb Oct 26 '09 at 18:40
  • 12
    Empty structs are a GCC extension. – Michael Burr Oct 26 '09 at 18:41
  • 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 Oct 26 '09 at 18:50
  • 4
    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 Oct 26 '09 at 18:51
  • Congrats on maxing your rep out on one answer... :P – GManNickG Oct 26 '09 at 18:55
  • "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 Oct 26 '09 at 19:03
  • Oh, thanks so much. I appreciate that you enjoy the rep festival with me. – Johannes Schaub - litb Oct 26 '09 at 19:05
6

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
  • 333,147
  • 50
  • 533
  • 760
5

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
  • 476,176
  • 80
  • 629
  • 1,111
  • "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. – AnT stands with Russia Oct 26 '09 at 18:48
  • 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 Oct 26 '09 at 19:01
  • 1
    It's a syntax rule violation, which demands a diagnostic message. – Johannes Schaub - litb Oct 26 '09 at 19:11
  • 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 Oct 26 '09 at 19:20
  • 1
    @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 Oct 26 '09 at 19:57
  • "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 Oct 26 '09 at 20:16
1

on VC 8 It gives error if we try to get the sizeof empty struct, on the other way round on linux with gcc it gives size 1 because it uses gcc extention instead of c language specification which says this is undefined behaviour.

struct node
{
// empty struct.
};

int main()
{
printf("%d", sizeof(struct node));
return 0;
}

on windows vc 2005 It gives compilation error on linux with gcc it gives size 1 because gcc extension http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Empty-Structures.html#Empty-Structures (As Pointed out by Michael Burr)

deven
  • 13
  • 5