Flexible array members in a union

Hello,

I hope this is the right forum for the question below.

In the DPDK project we're using clang as the compiler on Windows.

I noticed that there is a size difference between a Windows and a Linux if flexible array members are defined in a union inside a struct.

e.g.

struct umr {
  uint32_t temp;
  union {
    uint32_t res_dp[0];
    uint32_t res_dp2[0];
  };
};

On linux with GCC the result of

sizeof(struct umr) = 4

When on Windows with clang 11.0.0 I'm getting

sizeof(struct umr) = 8

Is this a bug? or flexible array members in a union behavior is undefined?

Thanks

Tal Shnaiderman.

In this instance, clang appears to be doing MSVC-compatible struct layout. See these examples on godbolt:
https://gcc.godbolt.org/z/3W47nTh8Y

MSVC will make this struct 8 bytes, GCC makes it 4. Clang follows suit depending on the target, *-windows-gnu or *-windows-msvc. That seems like intended behavior.

Thank you for the information, any idea why this difference exists?

I only found this in C11:

“In most situations, the flexible array member is ignored. In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply”

MSVC assigns the union a sizeof 4, and GCC assigns it a size of zero, and I think that’s all there is to it:
https://gcc.godbolt.org/z/1nhscrve9

I think if you drill down, GCC is allowing this union to be zero-sized, but MSVC ensures that the size is non-zero, and then rounds the size up to alignment to get 4.