Bug 7175 – Zero-length static array .ptr is always null

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-12-28T03:26:00Z
Last change time
2014-09-15T07:23:17Z
Keywords
pull
Assigned to
nobody
Creator
dlang-bugzilla
Depends on
12900

Comments

Comment #0 by dlang-bugzilla — 2011-12-28T03:26:59Z
void main() { struct S { ubyte[0] arr; } S s; assert(s.arr.ptr !is null); } s.arr.ptr should be the same as &s.
Comment #1 by yebblies — 2013-11-27T00:27:34Z
Comment #2 by bearophile_hugs — 2013-11-27T00:46:46Z
Do you mean in this code: import core.stdc.stdlib, std.stdio; struct VariableLength(T) { size_t len; T[0] items; @property T[] data() inout pure nothrow { return (cast(T*)&items)[0 .. len]; } static typeof(this)* New(in size_t len) nothrow { auto vptr = cast(typeof(this)*)calloc(1, typeof(this).sizeof + T.sizeof * len); vptr.len = len; return vptr; } alias data this; } void main() { enum n = 0100; auto v = VariableLength!int.New(n); foreach (immutable i; 0 .. n) (*v)[i] = i * 10; foreach (immutable i; 0 .. n) writeln((*v)[i]); } The data() function will be written just like this? @property T[] data() inout pure nothrow { return items.ptr[0 .. len]; }
Comment #3 by yebblies — 2013-11-27T01:37:22Z
(In reply to comment #2) > Do you mean in this code: > > [snip] > > > The data() function will be written just like this? > > @property T[] data() inout pure nothrow { > return items.ptr[0 .. len]; > } Looks like you'll still need to cast away inout in that example, but yeah. Where a is a zero-length static array a.ptr -> &a
Comment #4 by github-bugzilla — 2013-12-01T17:57:27Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/36c9b648a5701c4a9a73f3b1abf33e9a18e54839 Fix Issue 7175 - Zero-length static array .ptr is always null https://github.com/D-Programming-Language/dmd/commit/7652cfc7e279a2c23e4921bf52d46a6d663af253 Merge pull request #2888 from yebblies/issue7175 Issue 7175 - Zero-length static array .ptr is always null
Comment #5 by github-bugzilla — 2013-12-10T15:34:19Z
Comment #6 by github-bugzilla — 2014-09-15T07:23:17Z
Commit pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/b7866eff1567e5dfdf0e28e99d6cb3515d273854 Fix bogus zero-length array test. Issue 7175 was about .ptr always giving null for a zero-length static array in a struct, despite the fact that the struct has size 1 and thus certainly a non-null address. The test case as modified by 36c9b64, however, checks that a free-standing zero-length static array on the stack has an address, which is quite a different scenario. From the spec, I don't see a reason why zero-sized objects should need any memory associated with them, so I am reluctant to pessimize LDC's codegen over this.