dandelion said:
Define a v-table manually. I.e. define a struct containing
pointer-to-functions for methods and and the *static* data-members c'tor and
d'tor.
Add: Use wrapper functions for malloc, calloc and free.
A garbage collector handles dangling references,
including cyclic references, by freeing a dynamically
allocated chunk of memory when the last reference by
a live thread becomes unreachable. By definition,
a GC frees memory that the programmer cannot (or
forgot to) free. A GC is generally implemented as
a separate thread that periodically scans the call
stacks of every live thread to identify the unreachable
dynamic memory objects.
This presumes that there is a notion of a "thread"
in C. It also presumes that there is a way to scan
the call stack of each live thread to find the
"starting points" (i.e., local variables) that point
to dynamic memory. The latter presumption is generally
impossible with most C compiler implementations, because
the locations of local variables within a single stack
frame can be overlayed by block-scoped declarations.
#include <stdint.h>
void poo(void)
{
{
int * foo; /* [1] */
foo = malloc(sizeof *foo);
}
/* At this point, a garbage collector will
notice that the dynamic memory is unreachable
and perform an automatic free(foo).
*/
{
uintptr_t foo; /* likely at same location as [1] */
foo = 0xdeadbeef; /* quite likely overlays [1] */
/* There is no way at the language level
to retrieve [1] and free it.
*/
}
}
Thus, there is no way to really know the difference
between an integer variable and a pointer variable
that happen to occupy the same stack frame location
(at different times during execution). The C compiler
implementation would have to help the garbage collector;
it's not something that can be done at the language
level.
--
----------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS for FREE!