[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua define ARRAY_FLEXIBLE_MEMBER
- From: Roberto Ierusalimschy <roberto@...>
- Date: Mon, 14 Nov 2022 17:58:29 -0300
> "char data[0]" is not supported by the C standard, it's a GNU extension I
> believe.
>
> You can use 'offsetof' to determine the offset of a structure member, you
> can do something like
>
> struct STRING
> {
> size_t length;
> char data[1];
> }
>
> struct STRING* build_string(size_t size, const char* data)
> {
> struct STRING* p = (struct STRING*)malloc(offsetof(struct STRING, data) +
> size);
> p->length = size;
> memcpy(p->data, data, size);
> return p;
"char data[0]" is not standard C, and "char data[]" is not standard C89.
Lua uses exactly the technique described here, with offsetof, to
allocate Udata, CClosure, and LClosure without wasting memory.
-- Roberto