www.digitalmars.com         C & C++   DMDScript  

D - Slices and memory

reply "Vathix" <vathix dprogramming.com> writes:
I think if I did this:

char[] foo = new char[8888];
foo = foo[8 .. 88];

all 8888 bytes would remain in memory until foo (and any other references)
was no longer referring to a part of it.

I was wondering what the best way to prevent all that wasted memory would
be. Should I have done foo = foo[8 .. 88].dup (even if it was a lot more
memory and inefficient) or could I do something with the gc module so it'd
know to reclaim the parts around what I'm still using?
Aug 19 2003
parent "Walter" <walter digitalmars.com> writes:
"Vathix" <vathix dprogramming.com> wrote in message
news:bhsr13$nt8$1 digitaldaemon.com...
 I think if I did this:

 char[] foo = new char[8888];
 foo = foo[8 .. 88];

 all 8888 bytes would remain in memory until foo (and any other references)
 was no longer referring to a part of it.
Correct.
 I was wondering what the best way to prevent all that wasted memory would
 be. Should I have done foo = foo[8 .. 88].dup (even if it was a lot more
 memory and inefficient) or could I do something with the gc module so it'd
 know to reclaim the parts around what I'm still using?
The gc regards allocated memory blocks as indivisible. Using .dup would be the right approach in this case.
Aug 21 2003