D-
Jonathan M Davis
jmdavisProg at gmx.com
Sat Feb 11 10:45:31 PST 2012
On Saturday, February 11, 2012 19:00:51 q66 wrote:
> What's so difficult on that? Slices do not require the GC, you
> allocate once, slice many times, deallocate once.
With dynamic arrays, the GC owns the memory, and _all_ dynamic arrays are
slices. _None_ of them own their own memory. The GC keeps track of whether you
have any slices for a particular memory block and deals with freeing up wth
the block if you don't. However, if you allocate a dynamic array without the
GC, then all of a sudden, it effectively owns its own memory, so the semantics
of dealing with arrays and memories changes drastically. What happens when you
slice it? Which one owns the memory? What happens when you try and do stuff
like popFront on an array? All of a sudden, you have memory which is no longer
referenced. It's been leaked.
If you have a very careful scheme for handling memory, you _can_ slice arrays
without a GC, but you have to worry about all the bookkeeping of keeping track
of the originally allocated memory and how many slices reference it so that
you can free it when appropriate.
Also, you can't concatenate to arrays at all, because that requires the GC.
So, you're dealing with a mine field if you try and use D's array capabilities
without a GC. Yes, you _can_ use some of them if you're _very_ careful, but
I'd seriously advise just sticking to using arrays like you would in C except
for the fact that arrays know their length.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list