1

Consider the following code:

#include <stdio.h>

struct Empty1 {
} __attribute__((packed));

struct Empty2 {
        size_t data[0];
} __attribute__((packed));

int main(int argc, char *argv[])
{
        printf("sizeof(Empty1)=%zd\n", sizeof(struct Empty1));
        printf("sizeof(Empty2)=%zd\n", sizeof(struct Empty2));
        return 0;
}

Compiling it and running it with g++ (7.3) prints:

sizeof(Empty1)=1
sizeof(Empty2)=0

Which seems strange to me, since I would expect sizeof(Empty1) to also be 0.

what's more strange to me, however, is that compiling and running the same code with gcc (7.3) prints:

sizeof(Empty1)=0
sizeof(Empty2)=0

The fact that these two structures have different sizes depending on how they are compiled, seems like a bug to me, since it breaks the ABI between programs written in C or C++ that share headers. Not sure what is specified by the spec in this cases, but even as an implementation this looks strange to me.

ynimous
  • 4,642
  • 6
  • 27
  • 43

0 Answers0