This is the mail archive of the
mailing list for the GCC project.
Re: What does zero-length array mean at file scope?
Dave Korn wrote:
> Andrew Haley wrote:
>> Dave Korn wrote:
>>> Dave Korn wrote:
>>>> Dave Korn wrote:
>>>>> ELF GAS/LD seem happy enough when presented with a ".comm foo,0"
>>>>> directive, but PE does rather literally what you asked, and gives you
>>>>> no data object, leading to i0 in the above being an undefined
>>>>> reference at link time.
>>>> After a bit further digging, it turns out that this is because PE uses
>>>> the same representation for an external symbol as would represent a
>>>> common of size 0, so if you define such a common, it magically becomes
>>>> an undefined external symbol instead!
>> I don't see what the big deal is here: just allocate one byte to a
>> statically- allocated zero-length array.
> The "big deal" is that this is blatantly and trivially invalid code, and we
> silently accept it and generate nonsensical assembler output without the least
> hint of any kind of a diagnostic.
Of course we have to fix the assembler output. For ant two declarations
a and b, &a != &b, even when a is a zero-length array. So, you have to
allocate at least one byte.
> " 18.104.22.168 Array declarators
> 1 In addition to optional type qualifiers and the keyword static, the [ and
> ] may delimit an expression or *. If they delimit an expression (which
> specifies the size of an array), the expression shall have an integer type. If
> the expression is a constant expression, it shall have a value greater than
> zero. "
But zero-length arrays are a gcc extension. There's nothing that limits
them to the last member of a struct. zero-length arrays must be rejected
with -pedantic, but not otherwise.
>> Sure, but in that case it's clearly the user's fault: they're writing
>> beyond the bounds of an array.
> But how, as a user, would you attempt to write /within/ the bounds of such
> an array?
> Exactly. So why should we let the user create this meaningless
> construct in the first place?
Because it's a documented gcc extension.