[cfe-dev] Flexible array members in a union

Tal Shnaiderman via cfe-dev cfe-dev at lists.llvm.org
Fri Sep 10 09:58:51 PDT 2021

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"

From: Reid Kleckner <rnk at google.com>
Sent: Wednesday, September 8, 2021 11:09 PM
To: Tal Shnaiderman <talshn at nvidia.com>
Cc: cfe-dev at lists.llvm.org
Subject: Re: [cfe-dev] Flexible array members in a union

External email: Use caution opening links or attachments

In this instance, clang appears to be doing MSVC-compatible struct layout. See these examples on godbolt:

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.

On Tue, Sep 7, 2021 at 1:56 PM Tal Shnaiderman via cfe-dev <cfe-dev at lists.llvm.org<mailto:cfe-dev at lists.llvm.org>> wrote:

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.


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?


Tal Shnaiderman.
cfe-dev mailing list
cfe-dev at lists.llvm.org<mailto:cfe-dev at lists.llvm.org>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20210910/78dfca24/attachment.html>

More information about the cfe-dev mailing list