Vtables
A vtable is a struct of function pointers shared by every instance of one implementation. Each object holds a pointer to the table. A call through self->vptr->draw(self) is virtual dispatch, built by hand.
Detailed Explanation
C++ stores a hidden pointer to a compiler-generated table of methods. C lets you write that pointer yourself. The table type lists the operations:
struct ShapeVTable {
void (*draw)(struct Shape *self);
double (*area)(const struct Shape *self);
void (*destroy)(struct Shape *self);
};
The base object leads with a pointer to that table:
struct Shape {
const struct ShapeVTable *vptr;
};
A concrete type embeds the base as its first member, so a Circle * can be converted to a Shape * by taking the address of the first member, which is also the address of the circle. The circle's constructor sets vptr to a file-scope const table whose function pointers are the circle implementations.
Dispatch is one extra load: shape->vptr->draw(shape). Every circle shares one table, so the per-object cost is a single pointer, not one pointer per method. That is the reason to prefer a vtable over storing each function on the instance once you have more than a couple of operations or more than a couple of objects.
Destruction has to be virtual if the caller holds only a Shape *. shape->vptr->destroy(shape) knows to free the circle's extra fields. A free(shape) that ignores the table leaks those fields or, worse, frees the wrong size.
The first-member embedding is a convention, not a language rule. If you insert a field before the Shape base, the cast is wrong and you will corrupt memory. Some code uses a pointer to the base instead of embedding it; then the conversion is an ordinary field access and the object is no longer layout-compatible with the base.
Code Examples
#include <stdio.h>
#include <stdlib.h>
struct Shape;
struct ShapeVTable {
double (*area)(const struct Shape *self);
void (*destroy)(struct Shape *self);
};
struct Shape {
const struct ShapeVTable *vptr;
};
struct Circle {
struct Shape base; /* must be first for a safe upcast */
double radius;
};
static double circle_area(const struct Shape *self)
{
const struct Circle *c = (const struct Circle *)self;
return 3.14 * c->radius * c->radius;
}
static void circle_destroy(struct Shape *self)
{
free(self);
}
static const struct ShapeVTable circle_vt = {
.area = circle_area,
.destroy = circle_destroy,
};
static struct Shape *circle_new(double radius)
{
struct Circle *c = malloc(sizeof *c);
if (c == NULL) {
return NULL;
}
c->base.vptr = &circle_vt;
c->radius = radius;
return &c->base;
}
int main(void)
{
struct Shape *s = circle_new(2.0);
if (s == NULL) {
return 1;
}
printf("%.2f\n", s->vptr->area(s)); /* prints: 12.56 */
s->vptr->destroy(s);
return 0;
}Common Gotchas
The base struct must be the first member if you cast the outer pointer to the base pointer
any other layout makes the vptr load read the wrong bytes.
Destroy through the vtable, not with a raw `free`, when the caller has only the base type
the concrete type may own extra allocations.
A `const` file-scope vtable is the usual choice
every instance shares it, and nothing writes the function pointers at run time.
There is no type check on the down-cast inside a method
`circle_area` assumes `self` really is a `Circle`. Installing the wrong vtable is undefined in practice and silent.