Zero-length static array spec

David Nadlinger via Digitalmars-d digitalmars-d at puremagic.com
Fri Feb 6 08:15:19 PST 2015


On Monday, 2 February 2015 at 14:32:16 UTC, Steven Schveighoffer 
wrote:
> The lynch pin here is that "it's useful as the last member of a 
> variable length struct." If it's given the address of 0, then 
> it's no longer useful there, so it should take an address of 
> where it is defined.

This use case (i.e. as part of a bigger aggregate) already works 
in LDC.

The issue is specific to zero-size stack allocations. Last time I 
checked it wasn't possible to do this with LLVM in a sensible way 
without allocating a non-zero amount of memory. In contrast to 
the current behavior, this would actually violate the spec as it 
is worded now.

We can certainly discuss if and how to amend the spec to cater 
for certain use cases. However, this might be more tricky than it 
seems at first. For instance, since a pointer to a zero-length 
static array can never be legally dereferenced, we could just as 
well always make .ptr return 0xcafebabe for all cases where the 
array is not part of a larger object. Clearly this seems rather 
absurd, but how would you want to tighten down the spec?

Recall that in a C-like memory model, no meaning is attached to 
the numerical value of a pointer per se, and it is undefined 
behavior to perform arithmetic or comparisons between pointers 
that refer to different allocations. Formalizing anything beyond 
that is certainly not a task I want to tackle, and I suspect it 
would be a portability nightmare regarding modern compiler 
backends.

Cheers,
David


More information about the Digitalmars-d mailing list