35

I have two structs defined as follows:

struct EmptyStruct{

};

struct StructEmptyArr{
    int arr[0];
};

int main(void){
    printf("sizeof(EmptyStruct) = %ld\n", sizeof(EmptyStruct));
    printf("sizeof(StructEmptyArr) = %ld\n", sizeof(StructEmptyArr));

    return 0;
}

Compiled with gcc (g++) 4.8.4 on Ubuntu 14.04, x64.

Output (for both gcc and g++):

sizeof(EmptyStruct) = 1
sizeof(StructEmptyArr) = 0

I can understand why sizeof(EmptyStruct) equals to 1 but cannot understand why sizeof(StructEmptyArr) equals to 0. Why are there differences between two?

11
  • 13
    The latter is not a valid struct, so the compiler makes up a value of its own by adding features to the c++ language. The idea probably is that sizeof(struct)+sizeof(array) will yield a pointer just past the array and (char*)structpointer+sizeof(struct) gives the first element of the array. Commented Jul 19, 2017 at 15:19
  • 1
    Related to: stackoverflow.com/questions/1626446/…, stackoverflow.com/questions/9722632/… Commented Jul 19, 2017 at 15:24
  • 2
    Interesting, because I just did this with gcc version 6.3.0 and got an output of 0 for both. Commented Jul 19, 2017 at 15:25
  • 7
    @duong_dajgja: For standard C, StructEmptyArr is a syntax error (there must be at least one member in the structure C11 §6.7.2.1 ¶2), as is EmptyStruct (zero size arrays are not permitted in standard C; C11 §6.7.6.2 ¶1). GCC has extensions that may make them acceptable, but the standard says "No". Commented Jul 19, 2017 at 15:35
  • 5
    In passing: the result of sizeof operator is a size_t, so you should be using %zu or similar, not %ld. Commented Jul 19, 2017 at 15:50

2 Answers 2

40

In C, the behavior of a program is undefined if a struct is defined without any named member.

C11-§6.7.2.1:

If the struct-declaration-list does not contain any named members, either directly or via an anonymous structure or anonymous union, the behavior is undefined.

GCC allows an empty struct as an extension and its size will be 0.


For C++, the standard doesn't allow an object of size 0 and therefore sizof(EmptyStruct) returns a value of 1.
Arrays of zero length are not supported by standard C++¹, but are supported as an extension by GNU and the sizeof operator will return 0 if applied.


1. § 8.5.1-footnote 107) C++ does not have zero length arrays.

Sign up to request clarification or add additional context in comments.

10 Comments

But he says that he understands the reason for why the empty struct has its size. And the struct that does have a named member has the confusing size.
@michael clearly the code is not ISO C, because he uses the tag withithout the tag namespace qualifier
@michael it is not moot because while apples are red, my shirt is blue and in ISO C structs must have a named member, none of this is relevant because the question is not about apples, ISO C, or my shirt
@JohannesSchaub-litb; Its not my hypothesis. Also: "Unless it is a bit-field (9.6), a most derived object shall have a non-zero size and shall occupy one or more bytes of storage. Base class subobjects may have zero size." § 1.8 (p5)
@hacks you say that the standard forbids objects of size 0, and go on to quote that it allows such. And link to a page that merely says that objects shall have different addresses. If I take numbers 9+0 and 8+0, both are different, yet I added 0 to them.
|
5

https://gcc.gnu.org/onlinedocs/gcc/Empty-Structures.html

G++ treats empty structures as if they had a single member of type char.

https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html

Zero-length arrays are allowed in GNU C. They are very useful as the last element of a structure that is really a header for a variable-length object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.