lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi

I see many posts regarding struct with variable size array.

I consider this article, written by Gnu C authors, enlightening on the topic:
       https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html


Extracts:
- declaring zero-length arrays is allowed in GNU C as an extension
- in ISO C90 the array would typically be declared to have a single element
- ISO C99 introduces flexible array member :  the array is declared without a size, just []


In my code, for struct with a last variable size element, I use:
#if (__STDC_VERSION__ >= 199901L)        // C99
      char szError[];
#else
      char szError[1];                                         // C90
#endif


Does Lua 5.3 / 5.4 support also pre C90 compilers ?
Is there any list about these compilers ?
It is useful to know which platform/compiler we loose using C90 features.

M


On Wed, 13 May 2020 at 05:11, 云风 Cloud Wu <cloudwu@gmail.com> wrote:
Roberto Ierusalimschy <roberto@inf.puc-rio.br> 于2020年5月13日周三 上午12:28写道:
>
> This code wastes up to seven bytes (due to alignment), compared with
> the original version. A better option would be to use 'offsetof' to
> compute the size of the rest of the structure:
>
> #define sizelstring(l)  (offsetof(TString, bytes) + ((l) + 1) * sizeof(char))

'offsetof' would be better. And gcc allows 'char bytes[0];' or 'char bytes[];' .

It's C99 standard, but MSVC doesn't support it :(

https://stackoverflow.com/questions/20221012/unsized-array-declaration-in-a-struct


--
http://blog.codingnow.com
_______________________________________________
lua-l mailing list -- lua-l@lists.lua.org
To unsubscribe send an email to lua-l-leave@lists.lua.org
_______________________________________________
lua-l mailing list -- lua-l@lists.lua.org
To unsubscribe send an email to lua-l-leave@lists.lua.org