From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 1011B3858D37; Thu, 25 May 2023 16:42:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1011B3858D37 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685032955; bh=MZE9mtwaBuS9gQ1YxDwMylGbktnNlGMgbco5/orGQYI=; h=From:To:Subject:Date:In-Reply-To:References:From; b=FVcdQHovp3I2K2JACXw+RHN/oRaSPlx50FwBnJmNNOEMunBj5rKgkLDFP9xqI7tID ktn51vhOZxp9rlEjD01tmO9Ax61VzeTQx4wzuQW2bDQ7zHHOfMSG6gG339Wyrwaju7 NtBnhKxoxXWSvTNNmlCxsH6OUT4Bf+oO6zB6WVA8= From: "muecker at gwdg dot de" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/109956] GCC reserves 9 bytes for struct s { int a; char b; char t[]; } x = {1, 2, 3}; Date: Thu, 25 May 2023 16:42:33 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: trivial X-Bugzilla-Who: muecker at gwdg dot de 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: Message-ID: In-Reply-To: References: 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 --- Comment #12 from Martin Uecker --- The C standard says "However, when a . (or -> ) operator has a left operand that is (a pointer to) a structure with a flexible array member and the rig= ht operand names that member, it behaves as if that member were replaced with = the longest array (with the same element type) that would not make the structure larger than the object being accessed;"=20 This would imply that also for GCC not all elements can be accessed in the following structure without invoking UB. For x86_64 'x' has 11 bytes (for c= lang 9) but a struct with replacement array actually needs 12. struct foo { int a; short b; char t[]; } x =3D { .t =3D { 1, 2, 3 } };=20 // x has 11 bytes struct bar { int a; short b; char t[3]; }; // 12 bytes One can argue that this does not matter for an extension and maybe the C standard should not be read in this way as itself also contains an example using the sizeof() + n * sizeof() rule which implies that this amount of storage is enough for n elements. But the question is what programmers should use when using memcpy of static= ally initialized structs which have a FAM? sizeof() + n * sizeof() works for G= CC but not for clang. sizeof(struct bar) works for neither. One can use MAX(sizeof(struct foo, offsetof(struct foo, t) + n * sizeof(char)) but I ha= ve not seen anybody use it.=