Composition
Composition builds a larger object by storing other objects inside it, as members. In C that means a struct field that is another struct, or a pointer to one, rather than an inheritance relationship the language does not have.
Detailed Explanation
C cannot say class Square extends Shape. It can say struct Square { struct Shape base; int side; }; or, more honestly for most designs, struct Connection { struct Logger log; struct Buffer in; struct Buffer out; };. The connection has a logger and two buffers. It does not pretend to be a logger.
Has-a is the default in C because it is the only relationship the type system understands. The outer struct's functions call the inner struct's functions. connection_init calls logger_init and buffer_init. connection_close calls them in reverse. Ownership is visible: if the members are by value, the outer object's storage contains the inner objects and their lifetimes match. If the members are pointers, the outer object may or may not own them, and the header has to say which.
Composition beats a pile of globals for the same reason objects do: you can have two connections. It beats a simulated inheritance tree when the extra type is a part, not a kind. A HttpClient has a socket and a TLS session. It is not a kind of socket. Embedding a struct Socket and calling socket_read is the design. Casting HttpClient * to Socket * is a different, sharper tool, reserved for the cases where you truly need a Socket * API.
The cost of by-value members is size and copy behavior. Assigning the outer struct copies the inner ones. If an inner member owns a heap buffer, you now have two structs that think they own it. Either forbid copies, provide a deep clone, or store pointers and document ownership.
The cost of pointer members is lifetime. If the logger is borrowed, the connection must not outlive it. If the logger is owned, connection_free must free it. C will not track this. Names help: log versus log_owned, or a comment on the field.
Code Examples
#include <stdio.h>
struct Buffer {
char data[8];
int len;
};
struct Connection {
struct Buffer in; /* has-a, by value: lifetimes match */
struct Buffer out;
int fd;
};
static void buffer_init(struct Buffer *b)
{
b->len = 0;
b->data[0] = '\0';
}
static void connection_init(struct Connection *c, int fd)
{
buffer_init(&c->in);
buffer_init(&c->out);
c->fd = fd;
}
int main(void)
{
struct Connection c;
connection_init(&c, 3);
printf("%d %d\n", c.fd, c.in.len); /* prints: 3 0 */
return 0;
}#include <stdio.h>
#include <stdlib.h>
struct Logger {
int writes;
};
struct Service {
struct Logger *log; /* borrowed: service does not free it */
};
static void service_run(struct Service *s)
{
s->log->writes += 1;
}
int main(void)
{
struct Logger log = { 0 };
struct Service svc = { &log };
service_run(&svc);
printf("%d\n", log.writes); /* prints: 1 */
return 0;
}Common Gotchas
A by-value member that owns heap memory makes the outer struct unsafe to copy
assignment aliases the allocation. Provide `init`/`cleanup` and treat copies as a bug.
A pointer member is nullable and may be borrowed or owned
if the comment does not say which, the destructor will be wrong.
Composition does not give you polymorphism
`struct Connection` is not a `struct Buffer`. Call the buffer functions on `&c.in`.
Initializing the outer struct with `{0}` does not run inner `init` functions that allocate
zero is a valid empty state only if you designed it that way.