3 ms·
TL;DR is the introduction of C99 VLAs, not Pascal-style arrays, though a potential attribute could be added so we could do int some_int; int some_array[] _
by tmtvl 4y ago
TL;DR is the introduction of C99 VLAs, not Pascal-style arrays, though a potential attribute could be added so we could do
int some_int;
int some_array[] __attribute__((__element_count__(some_int)));
to store the size of some_array in some_int.
- kevin_thibedeau 4y agoThis has nothing to do with VLAs. The article covers FAMs at the end of structs.
- tmtvl 4y agoI must have misunderstood, thank you for the clarification. I thought that having no size specifier in the array declaration turned it into a VLA, which could have repercussions when embedding structs, e.g.: struct { int a; /* ... */ int b[]; } foo; struct { struct foo; /* ... */ int c; } bar; I would expect to have issues when trying to access the other members of struct bar like c.
- deleted 4y ago[deleted]
- loeg 4y agoSibling already pointed out this article is not talking about VLAs, but I do like your flexible-array attribute extension proposal.
- xenadu02 4y agoYou could take this idea further by adding an "array_ref" type to the C standard where it is a length and elements. In practice this would be broken into separate parameters when passed to a function so existing functions could be fixed post-hoc using attributes - similar to printf formatting attributes. Then passing an array_ref to a function that expects a pointer/length would let the compiler automatically translate. Then you could define the rules for how an array_ref decays to a pointer and how to make one out of a pointer and length. In other words the C standard could make bounds-checking of arrays possible with a good interop story if the standards committee believed it was worth doing. Compilers would have a flag to enable or disable the runtime checks based on safety/perf tradeoffs. Libraries would slowly add the relevant annotations. Eventually most code would have the option of having all array accesses bounds checked.
- yyyk 4y agoSince clang will never support VLAIS, your proposition is a non-starter.