public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/109956] New: GCC reserves 9 bytes for struct s { int a; char b; char t[]; } x = {1, 2, 3};
@ 2023-05-24 16:09 pascal_cuoq at hotmail dot com
  2023-05-24 16:20 ` [Bug c/109956] " pinskia at gcc dot gnu.org
                   ` (15 more replies)
  0 siblings, 16 replies; 17+ messages in thread
From: pascal_cuoq at hotmail dot com @ 2023-05-24 16:09 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109956

            Bug ID: 109956
           Summary: GCC reserves 9 bytes for struct s { int a; char b;
                    char t[]; } x = {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 “struct with FAM” (flexible array member)
with an initializer for the FAM are a GCC extension.

As of GCC 13.1 and Compiler Explorer “trunk”, targeting x86, the definition
“struct s { int a; char b; char t[]; } x = {1, 2, 3};” reserves 9 bytes for x,
and in fact, with various initializers, the trailing padding for variables of
type “struct s” is always 3, as if the size to reserve for the variable was
computed as “sizeof (struct s) + n * sizeof(element)”.

Input file:
struct s { int a; char b; char t[]; } x = {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 multiple
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
“sizeof(t)” 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.

^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2024-03-14 20:17 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-24 16:09 [Bug c/109956] New: GCC reserves 9 bytes for struct s { int a; char b; char t[]; } x = {1, 2, 3}; pascal_cuoq at hotmail dot com
2023-05-24 16:20 ` [Bug c/109956] " pinskia at gcc dot gnu.org
2023-05-24 16:28 ` muecker at gwdg dot de
2023-05-24 18:30 ` pascal_cuoq at hotmail dot com
2023-05-24 19:18 ` muecker at gwdg dot de
2023-05-24 20:25 ` muecker at gwdg dot de
2023-05-24 21:50 ` joseph at codesourcery dot com
2023-05-24 21:55 ` joseph at codesourcery dot com
2023-05-25  5:23 ` amonakov at gcc dot gnu.org
2023-05-25  5:29 ` muecker at gwdg dot de
2023-05-25  5:30 ` pinskia at gcc dot gnu.org
2023-05-25  7:53 ` rguenth at gcc dot gnu.org
2023-05-25 16:42 ` muecker at gwdg dot de
2023-05-26 12:03 ` pascal_cuoq at hotmail dot com
2023-05-26 16:24 ` muecker at gwdg dot de
2023-08-08 14:57 ` muecker at gwdg dot de
2024-03-14 20:17 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).