From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 95A893858D3C; Fri, 26 May 2023 12:04:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 95A893858D3C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685102640; bh=k+5bFvANsVcEtM7OkIQOHfeUBb2x45S2/40Wjg0zXQ8=; h=From:To:Subject:Date:In-Reply-To:References:From; b=UnF4eKJhB8P9eV9dwfSskSyrlPJupGnOgtCXZqws6NOGdnBj09IRxeMplFGLnfSeu 6NhhrRf2OnpTn2Ku3L/gA9gXYDoZbJIrVpnI0kJfVTnPdsrSNGE+Gt5dB0z5y6T6pl 5tqTBEM93CXsHgANLyHEQ06Us025kU7YzQJvbXpA= From: "pascal_cuoq at hotmail dot com" 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: Fri, 26 May 2023 12:03:59 +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: 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: 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 #13 from Pascal Cuoq --- @Martin I completely agree with comment 12, however about the last paragraph, I wou= ld like to point out that for purposes of memcpy'ing to or from such a struct = with initialized FAM, it is enough to recommend that programmers use the simple formula =E2=80=9Coffsetof(struct foo, t) + n * sizeof(char)=E2=80=9D (or = =E2=80=9Coffsetof(struct foo, t[n])=E2=80=9D. The part that is not copied is the part that they did not i= ntend to use when they chose the initializer of the FAM, and that they cannot portably u= se because of the padding that may or may not exist for a different target architecture. So since: First, GCC currently does not always reserve enough room to allow =E2=80=9C= memcpy(=E2=80=A6, =E2=80=A6, sizeof(struct foo) + n * sizeof(char))=E2=80=9D, and=20 second, using the time-proven formula as argument of malloc technically does not always allocate enough space to make it valid to access p->t[n-1] accor= ding to the strict interpretation of the words =E2=80=9Cit behaves as if that me= mber were replaced with the longest array (with the same element type) that would not make the structure larger than the object being accessed=E2=80=9D, we might as well start recommending that C programmers use =E2=80=9Coffseto= f(struct foo, t) + n * sizeof(char)=E2=80=9D as argument of memcpy, and either clari= fy the meaning of the words =E2=80=9Cit behaves as if=E2=80=A6=E2=80=9D in the C s= tandard or prepare for a very unpleasant discussion when we have to tell them the formula they have = to use as argument of malloc.=