[Issue 9985] Postblit isn't called on local struct return

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Apr 25 00:56:12 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=9985



--- Comment #5 from Kenji Hara <k.hara.pg at gmail.com> 2013-04-25 00:56:11 PDT ---
(In reply to comment #0)
> For this program: http://dpaste.dzfl.pl/d73575a1

Don't link to external web site. Instead please directly paste the code in
comment, or attach code file.

// Code:
import std.stdio;

struct S
{
    ubyte* b;
    ubyte buf[128];

    this(this)
    {
        writeln("postblit");
    }
}

auto ref makeS()
{
    S s;
    s.b = s.buf;
    writeln("made S at ", cast(void*)&s, ", s.b == ", s.b);
    return s;
}

void main() 
{
    S s = makeS();
    writeln("got back S at ", cast(void*)&s, ", s.b == ", s.b);
}

> I get
> 
>     made S at 18FC64, s.b == 18FC68
>     got back S at 18FCF4, s.b == 18FC68
> 
> as output. Note that no "postblit" message was printed.
> Patching s.b to point into the newly allocated struct in postblit is crucial
> here, but it seems like the postblit constructor isn't called, nor is there any
> attempt 
> to optimize away the temporary in `makeS()` even with -O.
> Am I doing something wrong?
> 
> http://dpaste.dzfl.pl/cc460feb

// Code:
import std.stdio;

struct S
{
    ubyte* b;
    ubyte buf[128];

    this(this)
    {
        writeln("postblit");
    }
}

auto ref makeS()
{
    S s;
    s.b = s.buf;
    writeln("made S at ", cast(void*)&s, ", s.b == ", s.b);
    return s;
}

void main() 
{
    S s = makeS();
    writeln("got back S at ", cast(void*)&s, ", s.b == ", s.b);
}

> copies the struct and ivokes postblit just fine.

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


More information about the Digitalmars-d-bugs mailing list