GC and slices

Sean Kelly sean at f4.ca
Thu Sep 14 08:48:48 PDT 2006


Oskar Linde wrote:
> This is a very short question regarding the liberties of the GC. Is it 
> safe to rely on data outside a slice?
> 
> For example, given:
> 
> int* a = new int[100_000] + 50_000;
> std.gc.fullCollect();
> 
> I'd still assume both a[-50_000] and a[49_999] to remain valid.
> 
> But does the same hold for slices?

Yes it does.  I imagine it might be possible to create an allocator that 
shrinks arrays which only have partial slice references to them, but it 
seems far too complicated to be worthwhile.

> int[] a = (new int[100_000])[50_000..50_010];
> std.gc.fullCollect();
> 
> Is the memory outside the slice 'a' still guaranteed to be valid, or is 
> a compacting GC allowed to remove the unreferenced areas outside the slice?

It might be useful to add formal language stating that the entire block 
will remain valid, but it's a safe assumption to operate under either 
way.  I'll try to add something to this effect in the docs for Ares' 
std.memory module.


Sean



More information about the Digitalmars-d mailing list