Different behaviour of new and malloc

Namespace rswhite4 at googlemail.com
Sat Apr 13 15:43:47 PDT 2013


On Saturday, 13 April 2013 at 22:18:21 UTC, bearophile wrote:
> Namespace:
>> I have a problem and maybe one you can explain, why this 
>> failure happen.
>
> Consider using something like:
>
> auto newPixel = orgPixel[0 .. this.width * this.height * 
> this.depth].dup;
>
> Bye,
> bearophile

Nice idea. That works too.
Here an overview:

ubyte* before = cast(ubyte*) this.getPixels();

size_t size = this.width * this.height * 4;
ubyte* after = cast(ubyte*) GC.malloc(ubyte.sizeof * size); /// 
works
//ubyte[] after = before[0 .. size].dup; /// works
//ubyte[] after = new ubyte[size]; /// doesn't work
memcpy(after/*.ptr*/, before, size);

It's a bit strange.


More information about the Digitalmars-d-learn mailing list