[Issue 7175] Zero-length static array .ptr is always null

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Nov 27 00:46:49 PST 2013


https://d.puremagic.com/issues/show_bug.cgi?id=7175


bearophile_hugs at eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs at eml.cc


--- Comment #2 from bearophile_hugs at eml.cc 2013-11-27 00:46:46 PST ---
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];
    }

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list