March 29, 2015 Re: Minor issue - zero-length fixed size arrays in variable-sized structs.. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | On Wednesday, 8 July 2009 at 22:55:47 UTC, Jarrett Billingsley wrote: > I noticed in the spec on arrays that "A [fixed-size] array with a > dimension of 0 is allowed, but no space is allocated for it. It's > useful as the last member of a variable length struct.." This sounds > like C99's "flexible array members," where a struct can have an array > as its last element that isn't given a size, specifically for allowing > variable-sized structs. > > Well, the issue with a zero-length fixed-size array is that.. uh, you > can't access anything out of it. The compiler disallows any indexing > of a zero-length array with constant indices, and at runtime, all > accesses caught by the array bounds checking. Weirder still, the .ptr > of any zero-length array is always null, so you can't even do things > like "arr.ptr[5] = x;" (which would be perfectly acceptable in my > opinion). > > Just a silly issue. Just thought I'd mention that this works now, since this is the only forum thread that mentions working with C's flexible array members. You simply define a zero-length array and then access it using .ptr, as in this C binding and example program I recently translated: https://github.com/joakim-noah/usrsctp/blob/master/usrsctp.d#L185 https://github.com/joakim-noah/usrsctp/blob/master/programs/rtcweb.d#L836 |
Copyright © 1999-2021 by the D Language Foundation