Init and Cleanup
Init and cleanup are the constructor and destructor you write yourself. One function puts an object into a usable state. A matching function releases what it owns. C will not call them for you.
Detailed Explanation
A C struct starts as uninitialized bytes, or as zeros if it has static duration or you wrote {0}. Neither of those runs your invariants. widget_init(&w) sets fields, acquires a file descriptor, allocates a buffer, and returns a success-or-failure code. widget_cleanup(&w) closes the descriptor, frees the buffer, and leaves the struct in a state you must not use — or a state init can be called on again, if you document that.
Two API shapes are common.
The in-place pair: the caller provides storage (Widget w; or a field inside another struct). init cannot fail to allocate the Widget itself; it can still fail to allocate what the widget owns. This is the right shape for composition by value and for embedded systems without a heap.
The heap pair: widget_new allocates the Widget and inits it. widget_free cleans up and frees the Widget. This is the right shape for opaque types, because the caller cannot see sizeof(Widget).
Failure mid-init is the case that produces leaks. If you open a file and then malloc fails, init must close the file before returning an error. The usual pattern is a single cleanup label, or a chain of goto fail that undoes the steps that succeeded. Do not leave that to the caller; the caller never received a valid object.
Cleanup must be safe on a zeroed or already-cleaned object if you can make it so: free(NULL) is a no-op, close(-1) is not. Set owned pointers to NULL and descriptors to -1 after release so a second cleanup is harmless. That idempotence is what makes error paths writable.
There is no finally. A function that inits two widgets and returns early must cleanup the ones that succeeded. Reviewers look for that pairing the way they look for malloc / free.
Code Examples
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Widget {
char *name;
int fd;
};
static int widget_init(struct Widget *w, const char *name)
{
w->name = NULL;
w->fd = -1;
w->name = malloc(strlen(name) + 1);
if (w->name == NULL) {
return -1;
}
memcpy(w->name, name, strlen(name) + 1);
return 0;
}
static void widget_cleanup(struct Widget *w)
{
free(w->name);
w->name = NULL;
w->fd = -1;
}
int main(void)
{
struct Widget w;
if (widget_init(&w, "alpha") != 0) {
return 1;
}
printf("%s\n", w.name); /* prints: alpha */
widget_cleanup(&w);
widget_cleanup(&w); /* second call is safe */
return 0;
}#include <stdlib.h>
#include <string.h>
struct Widget {
char *name;
};
static struct Widget *widget_new(const char *name)
{
struct Widget *w = malloc(sizeof *w);
if (w == NULL) {
return NULL;
}
w->name = malloc(strlen(name) + 1);
if (w->name == NULL) {
free(w); /* undo the object if a member fails */
return NULL;
}
memcpy(w->name, name, strlen(name) + 1);
return w;
}
static void widget_free(struct Widget *w)
{
if (w == NULL) {
return;
}
free(w->name);
free(w);
}Common Gotchas
Returning from `init` after a partial success without undoing it leaks
every acquired resource needs a matching release on the failure path.
`cleanup` on an uninitialized object is undefined if it `free`s a wild pointer
zero the struct, or have `init` set owned fields to `NULL` before the first acquisition.
C does not run `cleanup` at the end of a block
an early `return` skips it. Structure the function so every path that inits also cleans up.
`{0}` is not `init`
it is only enough when zero is a valid empty state and no resource is held.