pointers-memory · intermediate · ~15 min
Implement shared ownership and lifecycle management using reference counts and destructor callbacks.
Reference counting is a memory management strategy where an object tracks how many owners hold references to it. When the last reference is released, a destructor callback is automatically executed.
Implement:
struct rc_box {
int ref_count;
int data;
};
void rc_init(struct rc_box *b, int initial_val);
void rc_retain(struct rc_box *b);
int rc_release(struct rc_box *b, void (*on_destroy)(struct rc_box *));
rc_init: sets b->ref_count = 1 and b->data = initial_val.rc_retain: if b != NULL and b->ref_count > 0, increments b->ref_count++.rc_release:b == NULL or b->ref_count <= 0, return -1 (invalid/double-release).b->ref_count--.b->ref_count == 0: invoke on_destroy(b) (if non-NULL) and return 0.b->ref_count > 0: return b->ref_count.b: pointer to rc_box; initial_val: payload; on_destroy: callback function pointer.
rc_release returns 0 when destroyed, remaining ref_count when alive, -1 on error.
C11 freestanding reference count lifecycle management.
#include <stddef.h>
/* The harness provides:
struct rc_box {
int ref_count;
int data;
};
*/
void rc_init(struct rc_box *b, int initial_val) {
(void)b; (void)initial_val;
}
void rc_retain(struct rc_box *b) {
(void)b;
}
int rc_release(struct rc_box *b, void (*on_destroy)(struct rc_box *)) {
(void)b; (void)on_destroy;
return -1;
}
Decrementing ref_count below zero; calling destructor while references remain.
Calling release when ref_count == 0 returns -1 (double-free prevention); on_destroy NULL is safe.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.