[Gc] strange problem: GC needs manually calling to GC_gcollect()?

Boehm, Hans hans.boehm at hp.com
Wed Jul 13 11:47:39 PDT 2005


> From: Zhang Le
> 
> Thanks for the reply. 
> 
> My understanding now is that it is unsafe to store 
> GC_MALLOCed memory pointer in malloced pointers, unless I 
> change all code to use GC_MALLOC/GC_FREE.
> 
>  I notice on the example page there is a warning statement 
> reads: "Interaction with the system malloc It is usually best 
> not to mix garbage-collected allocation with the system 
> malloc-free. If you do, you need to be careful not to store 
> pointers to the garbage-collected heap in memory allocated 
> with the system malloc."
> 
> This statement is so technical that I ignored it the first 
> time I read it :-)
And in an ideal world, that would probably be the wrong default.
But doing this the other way requires that the collector knows
a lot about malloc internals, particularly since we don't want
to trace malloc memory that's not currently allocated.
> 
> I have one more question. Will the memory be collected 
> correctly if I offset the pointer:
> double* p = GC_MALLOC(sizeof(double)*100);
> p += 50;
> // access p[-10] p[-20], p[30] etc.
> 
> Here  I offset p to allow the use of negative  index, and p 
> no longer points to the original memory address. Will p still 
> be collected?
> 
The answer is "It depends".  In the default configuration,
with GC_all_interior_pointers set, and assuming the offset pointer
still points into the object, the answer is yes, it will work.
If p is stored on the stack, and points inside the object, the
answer is also yes.  If p is stored elsewhere, and
GC_all_interior_pointers is not set, you need to look at
GC_register_displacement().

Hans 



More information about the Gc mailing list