[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:
https://gcc.godbolt.org/z/3W47nTh8Y<https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgcc.godbolt.org%2Fz%2F3W47nTh8Y&data=04%7C01%7Ctalshn%40nvidia.com%7C6397d22100d149e1d7ff08d9730475b1%7C43083d15727340c1b7db39efd9ccc17a%7C0%7C0%7C637667285259556326%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=DTQMq3TZICAvx2w4dZokuEh3RRRU%2BysiyUyIRKkHK2U%3D&reserved=0>

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:
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.
_______________________________________________
cfe-dev mailing list
cfe-dev at lists.llvm.org<mailto:cfe-dev at lists.llvm.org>
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev<https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.llvm.org%2Fcgi-bin%2Fmailman%2Flistinfo%2Fcfe-dev&data=04%7C01%7Ctalshn%40nvidia.com%7C6397d22100d149e1d7ff08d9730475b1%7C43083d15727340c1b7db39efd9ccc17a%7C0%7C0%7C637667285259556326%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=5uBrefHc2FRxnbg7T4rwW0zGPPgtwZPYtZrzts8i5Yg%3D&reserved=0>
-------------- 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