Re: sizeof struct with flexible array: when did it change?

Liste des GroupesRevenir à cl c  
Sujet : Re: sizeof struct with flexible array: when did it change?
De : jbrubake.362 (at) *nospam* orionarts.invalid (Jeremy Brubaker)
Groupes : comp.lang.c
Date : 09. Oct 2024, 13:55:42
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <ve5uge$2lc25$1@dont-email.me>
References : 1 2 3
User-Agent : slrn/1.0.3 (Linux)
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.

Date Sujet#  Auteur
7 Oct 24 * sizeof struct with flexible array: when did it change?6Kaz Kylheku
7 Oct 24 +- Re: sizeof struct with flexible array: when did it change?1Tim Rentsch
7 Oct 24 `* Re: sizeof struct with flexible array: when did it change?4Nick Bowler
8 Oct 24  +* Re: sizeof struct with flexible array: when did it change?2Kaz Kylheku
9 Oct 24  i`- Re: sizeof struct with flexible array: when did it change?1Jeremy Brubaker
14 Oct 24  `- Re: sizeof struct with flexible array: when did it change?1Tim Rentsch

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal