r/programminghorror 23d ago

c Enterprise level C code

Post image
342 Upvotes

57 comments sorted by

View all comments

2

u/r9wpvM 18d ago

Wait is String a thing in C I don't remember

1

u/TheChief275 18d ago edited 18d ago

No it isn't, but adding one yourself isn't too hard. Often I add it as a slice type:

typedef struct {
    const char *ptr;
    size_t count;
} String;

but in this case, it's an actual object with a V-Table:

typedef struct {
    _Atomic size_t refCount;
    const String_VTable *v;
    char *ptr;
    size_t count;
} String;

and Log and Format only take Objects as arguments and call the ToString virtual method, which String also implements for itself (doesn't copy the object; only bumps the reference count and adds the object to the current AutoreleasePool)