Name: Anonymous 2012-01-09 14:49
Why don't compilers just automatically put frees in source code instead of collecting garbage at runtime?
/* C reference counting library */
typedef struct {
size_t n;
unsigned char data[];
} *Refcount;
#define ref_data(r) (void *)&(r)->data
#define ref_data_typed(T,r) *(T *)&(r)->data
Refcount ref_alloc(size_t size) {
Refcount r = malloc(sizeof(int)+size);
if (!r) return NULL;
r->d = 0;
return r;
}
Refcount ref_copy(Refcount d, Refcount s) {
d = s;
s->n++;
};
Refcount ref_delete(Refcount d) {
d->n--;
if (!d->n) free(d);
d = NULL;
};