garbage collector

G

Ganesh

Hi,

How does a garbage collector (in C++), like mark and sweep, work on
heap
allocated arrays? In this case,

1. The only live variable on stack that points to the memory may be the
one that holds the starting address of the array. Or,

2. There could be a stack variable that points to an address that is,
say, in the middle of the array.

How does GC mark the entire array as live in these cases?

Thanks,
Ganesh
 
H

Howard

Ganesh said:
Hi,

How does a garbage collector (in C++), like mark and sweep, work on
heap
allocated arrays? In this case,

1. The only live variable on stack that points to the memory may be the
one that holds the starting address of the array. Or,

2. There could be a stack variable that points to an address that is,
say, in the middle of the array.

How does GC mark the entire array as live in these cases?

I wasn't aware there *was* any garbage collection in standard C++. If it's
something provided by one of your compilers, then you should probably ask in
a newsgroup or help files for that compiler.

-Howard
 
A

acehreli

Howard said:
How does a garbage collector (in C++), like mark and sweep, work on
heap
allocated arrays?
[...]
I wasn't aware there *was* any garbage collection in standard C++. If it's
something provided by one of your compilers, then you should probably ask in
a newsgroup or help files for that compiler.

Good advice, but GC is not necessarily a compiler feature in C or C++.
It can be implemented as a library:

http://www.hpl.hp.com/personal/Hans_Boehm/gc/

That page seems to contain the answers the OP is looking for.

Ali
 
G

Ganesh

I posted my question in this group because:

1. Searching for "Garbage Collection" in groups home, yielded
comp.lang.c++.moderated as the first link. Search for "Garabage
Collector" yeilded 3rd link to the same group. And there were atleast 3
links to this group in the first 10 results. Though, I posted it in
the 'moderated' version, it was not accepted. It is ridiculous, as
there is a disucussion in the same group on a very similar topic and
153 messages have been posted.

2. I used only C++ as my primary programming language. My question had
some assumptions, like having a pointer, which is true in C++ and not
JAVA, and hence I posted it here.

Ganesh
 
M

msalters

Ganesh schreef:
I posted my question in this group because:

1. Searching for "Garbage Collection" in groups home, yielded
comp.lang.c++.moderated as the first link. Search for "Garabage
Collector" yeilded 3rd link to the same group. And there were atleast 3
links to this group in the first 10 results. Though, I posted it in
the 'moderated' version, it was not accepted. It is ridiculous, as
there is a disucussion in the same group on a very similar topic and
153 messages have been posted.

GC isn't always offtopic, as it is possible to combine C++ and GC.
However,
blue-sky discussions about every possible combination wouldn't make
sense.
2. I used only C++ as my primary programming language. My question had
some assumptions, like having a pointer, which is true in C++ and not
JAVA, and hence I posted it here.

Actually, Java does have pointers, and they're more suited to GC use
than
C++ pointers (no reinterpret_cast<> between int and Object in Java)
There are a lot more languages which use pointers, even if they expose
them only to their internal GC mechanism. That too makes your question
a GC question, an not a C++ question.

Compare: a question about numerical computing is off-topic here, even
if it happened to be implemented in C++. valarray<> would be ontopic.

HTH,
Michiel Salters
 
B

Branimir Maksimovic

Ganesh said:
Hi,

How does a garbage collector (in C++), like mark and sweep, work on
heap
allocated arrays?

There is no difference between array of objects and single object.
They are all allocated as memory blocks.

In this case,
1. The only live variable on stack that points to the memory may be the
one that holds the starting address of the array. Or,

2. There could be a stack variable that points to an address that is,
say, in the middle of the array.

How does GC mark the entire array as live in these cases?

GC does not know about arrays. It just tracks pointers to memory area that
is
bound by GC allocated heap.
(I think that you asked about hans boehm gc implementation)
hope this helps.

Greetings, Bane.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top