C/C++ Arena

Step 9 of 9

Challenge: a string builder

Building a long string with repeated strcat into a fixed buffer is how buffer overflows happen. Real C code uses a string builder: a struct that owns a growing heap buffer.

typedef struct {
    char *buf;   /* always '\0'-terminated once initialized */
    size_t len;  /* characters used, not counting '\0' */
    size_t cap;  /* bytes allocated */
} Builder;

Your turn: write the four functions:

Previous: Who owns what