U
user923005
jacob navia said:Keith Thompson wrote:You should do
memset(ptr,0,size_of_ptr);
BEFORE.Now, many free() implementations fill the freed block with
zeroes or other values. IN any case, the GC will
NOT find the pointer when it scans for pointers and
everything will work.Yes. And it is very easy to see that if you want to get rid of
an object pointed to by p, the best thing is to just makep = NULL;and be done with it.
You were suggesting that GC is essentially a library issue and that
there is no change to the "core" language, but these two examples
suggest that a subtle change is required (or at least implied): for
GC to work well, every pointer must behave rather as if it were
volatile. In fact, all memory will have to be treated like that.
In "old C", a compiler can remove the memset in the sequence:
memset(ptr, 0, size_of_date);
free(ptr);
but in your C, that would be a mistake (well, sub-optimal, maybe).
Similar things apply to the 'ptr = NULL;' example. Are you proposing
to make this change to C's "as if" rules?
[BTW, is the GC you ship the Boehm one or your own?]
There are other problems with GC, when you have performance critical
operations.
If you are running a program and someone has used gc in a lib you are
using, then your program could 'hang' at inappropriate times when the
garbage collector is running.
It's a bigger problem than it sounds like. Although it is supposed to
be a bad idea, I find myself manually running the garbage collector in
GC languages to prevent inappropriate behavior (such as huge memory
consumption, very long pauses, etc.).