D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7175 - Zero-length static array .ptr is always null
Summary: Zero-length static array .ptr is always null
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: pull
Depends on: 12900
Blocks:
  Show dependency treegraph
 
Reported: 2011-12-28 03:26 UTC by Vladimir Panteleev
Modified: 2014-09-15 07:23 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Vladimir Panteleev 2011-12-28 03:26:59 UTC
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 2 bearophile_hugs 2013-11-27 00:46:46 UTC
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 yebblies 2013-11-27 01:37:22 UTC
(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 github-bugzilla 2013-12-01 17:57:27 UTC
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 github-bugzilla 2013-12-10 15:34:19 UTC
Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/3bf7718d82848f21b9e5562c96f3c7e8702c7ea7
Update message changed by fixing issue 7175
Comment 6 github-bugzilla 2014-09-15 07:23:17 UTC
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.