How the GC distinguishes code from data
"Jérôme M. Berger"
jeberger at free.fr
Thu Jan 6 10:52:19 PST 2011
%u wrote:
> Hi,
>
> There's a question that's been lurking in the back of my mind ever since I
> learned about D:
>
> How does the GC distinguish code from data when determining the objects to
> collect? (E.g. void[] from uint[], size_t from void*, etc.?)
>
> If I have a large uint[], it's practically guaranteed to have data that looks
> like pointers, and that might cause memory leaks. Furthermore, if the GC moves
> things around, it would corrupt my data. How is this handled?
>
> Thank you!
The GC knows about global variables, the stack, everything that was
allocated through it and everything that you tell it to scan (which
allows using C malloc without seeing an object disappear because the
only remaining pointers are in a malloc'ed buffer). Moreover, for
GC-allocated data (and maybe the globals too), the GC knows that
some data cannot contain pointers and will refrain from scanning it
(it will always assume that anything on the stack or that you tell
it to scan contains pointers).
The GC keeps track internally of the memory where it knows there
are no pointers and the memory where there may be pointers.
Jerome
--
mailto:jeberger at free.fr
http://jeberger.free.fr
Jabber: jeberger at jabber.fr
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: OpenPGP digital signature
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20110106/8c064b44/attachment.pgp>
More information about the Digitalmars-d-learn
mailing list