Safely extend the size of a malloced memory block after realloc
Steven Schveighoffer via Digitalmars-d
digitalmars-d at puremagic.com
Mon Aug 17 12:38:21 PDT 2015
On 8/17/15 3:27 PM, Benjamin Thaut wrote:
> Consider the following code
>
> void* mem = malloc(500);
> GC.addRange(mem, 500);
> mem = realloc(mem, 512); // assume the pointer didn't change
> GC.removeRange(mem);
This is actually unsafe, you have to remove the range first, or else if
it *does* change the pointer, your GC is using free'd memory. Plus, if
it does change the pointer, how do you remove the original range?
> // if the GC kicks in here we're f*****
> GC.addRange(mem, 512);
Can't you GC.disable around this whole thing?
-Steve
More information about the Digitalmars-d
mailing list