home bbs files messages ]

Just a sample of the Echomail archive

COMPLANC:

<< oldest | < older | list | newer > | newest >> ]

 Message 241,260 of 243,097 
 BGB to Kaz Kylheku 
 Re: Nice way of allocating flexible stru 
 08 Oct 25 11:33:45 
 
From: cr88192@gmail.com

On 10/8/2025 1:35 AM, Kaz Kylheku wrote:
> Jonas Lund of https://whizzter.woorlic.org/ mentioned this
> trick in a HackerNews comment:
>
> Given:
>
>    struct S {
>      // ...
>      T A[];
>    };
>
> Don't do this:
>
>    malloc(offsetof(S, A) + n * sizeof (T));
>
> But rather this:
>
>    malloc(offsetof(S, A[n]));
>
> It's easy to forget that the second argument of offsetof is a
> designator, not simply a member name.
>

This is assuming offsetof and can deal with general expressions (vs just
field names). IIRC, it is only required to work with field names (and
with plain structs).

--- SoupGate-Win32 v1.05
 * Origin: you cannot sedate... all the things you hate (1:229/2)

<< oldest | < older | list | newer > | newest >> ]


(c) 1994,  bbs@darkrealms.ca