From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 3931D3858D28; Wed, 24 May 2023 16:09:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3931D3858D28 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1684944574; bh=GKP3lvhqOOb1eEwtOV7tslQ88kvXRyndmINHKnq7rHc=; h=From:To:Subject:Date:From; b=PDXC9gPldG4uiON4d5rrT0j5cAXpLQs2u6EBeFoD/g95qrSrxWgVB3E60qUGDL7Js LfR/jMxwan9AYpwqMVvJ5sg0y+/usA46UIgom3RDRB7PC3a2BlBsYdtRK+i/R9TBH0 Pf6rTuKhH7/siR/jOAToV1sczUGPmum2dkkHUbCk= From: "pascal_cuoq at hotmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/109956] New: GCC reserves 9 bytes for struct s { int a; char b; char t[]; } x = {1, 2, 3}; Date: Wed, 24 May 2023 16:09:33 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: pascal_cuoq at hotmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D109956 Bug ID: 109956 Summary: GCC reserves 9 bytes for struct s { int a; char b; char t[]; } x =3D {1, 2, 3}; Product: gcc Version: 14.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: pascal_cuoq at hotmail dot com Target Milestone: --- Static-lifetime variables of type =E2=80=9Cstruct with FAM=E2=80=9D (flexib= le array member) with an initializer for the FAM are a GCC extension. As of GCC 13.1 and Compiler Explorer =E2=80=9Ctrunk=E2=80=9D, targeting x86= , the definition =E2=80=9Cstruct s { int a; char b; char t[]; } x =3D {1, 2, 3};=E2=80=9D re= serves 9 bytes for x, and in fact, with various initializers, the trailing padding for variables = of type =E2=80=9Cstruct s=E2=80=9D is always 3, as if the size to reserve for = the variable was computed as =E2=80=9Csizeof (struct s) + n * sizeof(element)=E2=80=9D. Input file: struct s { int a; char b; char t[]; } x =3D {1, 2, 3}; Command: gcc -S fam_init.c Result (with Ubuntu 9.4.0-1ubuntu1~20.04.1 which exhibits the same behavior= as the recent versions on Compiler Explorer): .align 8 .type x, @object .size x, 9 x: .long 1 .byte 2 .byte 3 .zero 3 Clang up to version 14 used to round up the size of the variable to a multi= ple of the alignment of the struct, but even this is not necessary. It is only necessary that the size reserved for a variable of type t is at least =E2=80=9Csizeof(t)=E2=80=9D bytes, and also to reserve enough space for the= initializer. Clang 15 and later uses the optimal formula: max(sizeof (struct s), offsetof(struct s, t[n])) Compiler Explorer link: https://gcc.godbolt.org/z/5W7h4KWT1 This ticket is to suggest that GCC uses the same optimal formula as Clang 15 and later.=