Can sizeof return 0 (zero)

后端 未结 10 1540
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-28 08:14

Is it possible for the sizeof operator to ever return 0 (zero) in C or C++? If it is possible, is it correct from a standards point of view?

相关标签:
10条回答
  • 2020-11-28 08:50

    In C++ an empty class or struct has a sizeof at least 1 by definition. From the C++ standard, 9/3 "Classes": "Complete objects and member subobjects of class type shall have nonzero size."

    In C an empty struct is not permitted, except by extension (or a flaw in the compiler).

    This is a consequence of the grammar (which requires that there be something inside the braces) along with this sentence from 6.7.2.1/7 "Structure and union specifiers": "If the struct-declaration-list contains no named members, the behavior is undefined".

    If a zero-sized structure is permitted, then it's a language extension (or a flaw in the compiler). For example, in GCC the extension is documented in "Structures with No Members", which says:

    GCC permits a C structure to have no members:

     struct empty {
     };
    

    The structure will have size zero. In C++, empty structures are part of the language. G++ treats empty structures as if they had a single member of type char.

    0 讨论(0)
  • 2020-11-28 08:50

    I think it never returns 0 in c , no empty structs is allowed

    0 讨论(0)
  • 2020-11-28 08:51

    Every object in C must have a unique address. Worded another way, an address must hold no more than one object of a given type (in order for pointer dereferencing to work). That being said, consider an 'empty' struct:

    struct emptyStruct {};
    

    and, more specifically, an array of them:

    struct emptyStruct array[10];
    struct emptyStruct* ptr = &array[0];
    

    If the objects were indeed empty (that is, if sizeof(struct emptyStruct) == 0), then ptr++ ==> (void*)ptr + sizeof(struct emptyStruct) ==> ptr, which doesn't make sense. Which object would *ptr then refer to, ptr[0] or ptr[1]?

    Even if a structure has no contents, the compiler should treat it as if it is one byte in length in order to maintain the "one address, one object" principle.

    The C language specification (section A7.4.8) words this requirement as

    when applied to a structure or union, the result (of the sizeof operator) is the number of bytes in the object, including any padding required to make the object tile an array

    Since a padding byte must be added to an "empty" object in order for it to work in an array, sizeof() must therefore return a value of at least 1 for any valid input.

    Edit: Section A8.3 of the C spec calls a struct without a list of members an incomplete type, and the definition of sizeof specifically states (with emphasis added):

    The operator (sizeof) may not be applied to an operand of function type, or of incomplete type, or to a bit-field.

    That would imply that using sizeof on an empty struct would be equally as invalid as using it on a data type that has not been defined. If your compiler allows the use of empty structs, be aware that using sizeof on them is not allowed as per the C spec. If your compiler allows you to do this anyway, understand that this is non-standard behavior that will not work on all compilers; do not rely on this behavior.

    Edit: See also this entry in Bjarne Stroustrup's FAQ.

    0 讨论(0)
  • 2020-11-28 08:51

    Empty structs, as isbadawi mentions. Also gcc allows arrays of 0 size:

    int a[0];
    sizeof(a);
    

    EDIT: After seeing the MSDN link, I tried the empty struct in VS2005 and sizeof did return 1. I'm not sure if that's a VS bug or if the spec is somehow flexible about that sort of thing

    0 讨论(0)
  • 2020-11-28 08:53
    typedef struct {
      int : 0;
    } x;
    
    x x1;
    x x2;
    

    Under MSVC 2010 (/Za /Wall):

    sizeof(x) == 4
    &x1 != &x2
    

    Under GCC (-ansi -pedantic -Wall) :

    sizeof(x) == 0
    &x1 != &x2
    

    i.e. Even though under GCC it has zero size, instances of the struct have distinct addresses.

    ANSI C (C89 and C99 - I haven't looked at C++) says "It shall be possible to express the address of each individual byte of an object uniquely." This seems ambiguous in the case of a zero-sized object, since it arguably has no bytes.

    Edit: "A bit-field declaration with no declarator, but only a colon and a width, indicates an unnamed bit-field. As a special case of this, a bit-field with a width of 0 indicates that no further bit-field is to be packed into the unit in which the previous bit-field, if any, was placed."

    0 讨论(0)
  • 2020-11-28 08:57

    If you have this :

    struct Foo {}; 
    struct Bar { Foo v[]; }
    

    g++ -ansi returns sizeof(Bar) == 0. As does the clang & intel compiler.

    However, this does not compile with gcc. I deduce it's a C++ extension.

    0 讨论(0)
提交回复
热议问题