Deutsch English Français Italiano |
<ve5uge$2lc25$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!news.roellig-ltd.de!news.mb-net.net!open-news-network.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Jeremy Brubaker <jbrubake.362@orionarts.invalid> Newsgroups: comp.lang.c Subject: Re: sizeof struct with flexible array: when did it change? Date: Wed, 9 Oct 2024 12:55:42 -0000 (UTC) Organization: A noiseless patient Spider Lines: 89 Message-ID: <ve5uge$2lc25$1@dont-email.me> References: <20241006192337.76@kylheku.com> <ve19g1$1orao$1@dont-email.me> <20241007160553.819@kylheku.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Wed, 09 Oct 2024 14:55:43 +0200 (CEST) Injection-Info: dont-email.me; posting-host="3a7c71e5d45886412559291a162f6abc"; logging-data="2797637"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/BkjWkAf673DEQZarAmCVP8uEVPNDCM5M=" User-Agent: slrn/1.0.3 (Linux) Cancel-Lock: sha1:9xgk2U/Ndx6GWK7XQnfhNUlpNzU= Bytes: 3737 On 2024-10-07, Kaz Kylheku wrote: > On 2024-10-07, Nick Bowler <nbowler@draconx.ca> wrote: >> On Mon, 7 Oct 2024 02:32:13 -0000 (UTC), Kaz Kylheku wrote: > What GCC seems to be doing is simply nothing special. When determining > the most strictly aligned member of the struct, it takes the flexible > array into account (the alignment of its element type). It otherwise > ignores it (or perhaps treats it as a size zero subobject). The > structure is padded after that for the sake of the most strictly > aligned member. > >>> Don't get burned: don't rely on the size of a flexible array struct. >>> Use the offsetof that flexible member. > > If the size is anything other than what the program expects, whether > it is larger or smaller, that breaks the program. > > For instance, if the wrong value is used when displacing a pointer to > the flexible member to recover a pointer to the struct. > > This issue showed up in exactly one program of mine in which I > experimented with using the flexible array member. > > It was reported by a user who ran into a crash. > As the user who had the pleasure of running into said crash, here is a brief demo of the sizes and addresses reported by my system (gcc 13.3.1) using both methods of determining the start of the struct: #include <stdio.h> #include <stdlib.h> #include <stddef.h> typedef struct dstr { int a; size_t b; int c; char str[]; } dstr; typedef struct ref { int a; size_t b; int c; } ref; #define old_dstr_of(str) ((dstr *) ((str) - sizeof (dstr))) #define new_dstr_of(s) ((dstr *) ((s) - offsetof (struct dstr, str))) int main (int argc, char ** argv) { dstr *ds = malloc (sizeof (dstr)); printf ("sizeof(int) %zu\n", sizeof (int)); printf ("sizeof(char) %zu\n", sizeof (char)); printf ("sizeof(size_t) %zu\n", sizeof (size_t)); printf ("sizeof(dstr) %zu\n", sizeof (dstr)); printf ("sizeof(ref) %zu\n", sizeof (ref)); puts (""); puts ("Addresses:"); printf ("ds %p\n", ds); printf ("ds->str %p\n", ds->str); printf ("old dstr_of %p\n", old_dstr_of(ds->str)); printf ("new dstr_of %p\n", new_dstr_of(ds->str)); } And the output on my machine: sizeof(int) 4 sizeof(char) 1 sizeof(size_t) 8 sizeof(dstr) 24 sizeof(ref) 24 Addresses: ds 0x9d62a0 ds->str 0x9d62b4 old dstr_of 0x9d629c new dstr_of 0x9d62a0 -- () www.asciiribbon.org | Jeremy Brubaker /\ - against html mail | јЬruЬаkе@оrіоnаrtѕ.іо / neonrex on IRC Success is something I will dress for when I get there, and not until.