Zero-length static array spec
Stefan Frijters via Digitalmars-d
digitalmars-d at puremagic.com
Sun Feb 1 06:42:31 PST 2015
So recently I ran into this discrepancy between the behaviour of
dmd (and gdc) and ldc2:
void test(int[] data)
in { assert(data, "data must be non-null."); }
body { }
void main() {
import std.stdio;
int[1] data1;
writeln(data1); // [0]
test(data1); // Passes
assert(data1.ptr !is null);
int[0] data0;
writeln(data0); // []
test(data0); // Passes with dmd and gdc, fails with ldc2
(2.066.1)
assert(data0.ptr !is null); // Passes with dmd and gdc, fails
with ldc2
}
I reported this as an issue at
https://github.com/ldc-developers/ldc/issues/831 and was asked to
check for a more definite answer. So, in light of recent
developments of trying to tighten up the D spec, does anyone have
any insight what the correct behaviour should be, and can it be
locked down in the spec?
Currently the D spec says [1]:
---
Static Arrays
int[3] s;
These are analogous to C arrays. Static arrays are distinguished
by having a length fixed at compile time.
The total size of a static array cannot exceed 16Mb. A dynamic
array should be used instead for such large arrays.
A static 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, or as the degenerate case of a template expansion.
Static arrays are value types. Unlike in C and D version 1,
static arrays are passed to functions by value. Static arrays can
also be returned by functions.
---
It does not seem to say whether a zero-length array should have a
valid address or not.
Thoughts?
[1] http://dlang.org/arrays.html
More information about the Digitalmars-d
mailing list