StringBuilder in C v2: Not a charged dance A follow-up on this. To quote by copy/paste:


  Lots of languages have something that lets you build up a dynamically-sized string with minimal overhead. C doesn't, and I found myself using code that did that manually in a couple of places, so I packaged it into a class. Note that I've only implemented functionality I'm using.


stringbuilder.h

It's the same as last time.

#ifndef CONCATEN_STRINGBUILDER_H
#define CONCATEN_STRINGBUILDER_H
#include &lt;stddef.h&gt;
#include &lt;stdbool.h&gt;

struct StringBuilder;
typedef struct StringBuilder *StringBuilder;
StringBuilder sb_new(size_t);
bool sb_append(StringBuilder, char);
char *sb_as_string(StringBuilder);
char *sb_free_copy(StringBuilder);
size_t sb_size(StringBuilder);
void sb_free(StringBuilder);

#endif //CONCATEN_STRINGBUILDER_H


stringbuilder.c

#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include "stringbuilder.h"

struct StringBuilder {
    char *mem;
    size_t count;
    size_t cap;
};
StringBuilder sb_new(size_t init_cap) {
    if (init_cap == 0) {
        return NULL;
    }
    StringBuilder ret = malloc(sizeof(struct StringBuilder));
    if (!ret) return NULL;
    ret-&gt;mem = calloc(init_cap, sizeof(char));
    if (!ret-&gt;mem) {
        free(ret);
        return NULL;
    }
    ret-&gt;cap = init_cap;
    ret-&gt;count = 0;
    return ret;
}
#define LOAD_FACTOR 2
bool sb_append(StringBuilder to, char c) {
    if (to-&gt;count + 1 == to-&gt;cap) {
        char *new_mem = realloc(to-&gt;mem, to-&gt;cap * LOAD_FACTOR);
        if (!new_mem) {
            return false;
        }
        memset(new_mem + to-&gt;cap, 0, to-&gt;cap);
        to-&gt;mem = new_mem;
        to-&gt;cap *= LOAD_FACTOR;
    }
    ++to-&gt;count;
    to-&gt;mem[to-&gt;count - 1] = c;
    return true;
}
char *sb_as_string(StringBuilder sb) {
    return sb-&gt;mem;
}
char *sb_free_copy(StringBuilder sb) {
    char *ret = malloc((sb-&gt;count + 1) * sizeof(char));
    if (!ret) {
        return NULL;
    }
    strcpy(ret, sb-&gt;mem);
    ret[sb-&gt;count] = 0;
    sb_free(sb);
    return ret;
}
size_t sb_size(StringBuilder sb) {
    return sb-&gt;count;
}
void sb_free(StringBuilder sb) {
    if (sb) {
        if (sb-&gt;mem) free(sb-&gt;mem);
        free(sb);
    }
}


This time around, I'm concerned most about... exactly the same things:


  
  Performance. This code gets called a lot. I want it to be as fast as possible.
  Memory safety. While I'm fairly sure that this doesn't leak memory (assuming it's used properly), I'm not confident, and I'm not
  sure how to check.
  Edge cases. It works, as far as I can tell, but that doesn't mean that it's bug-free.
  


Also,


Portability. Will this code be compileable on any (C11-conforming and/or major) compiler? I want the code that uses this to be as simple to deploy on as many platforms as possible. 


Note that I specifically am not asking for advice on functions that would be nice to have. I'm only spending the time to implementing things I'm going to use; this isn't part of a general-purpose utility library, so I really don't care about supporting all the features when I use a tiny fraction of them.
The main reason why C don't have a system like that is because that produce some overhead. But I suppose you know this and want the less overhead possible and this system.

The two following lines are not C idiomatic:

struct StringBuilder;
typedef struct StringBuilder *StringBuilder;


Of course, they are two side about this, I will give you my vision. You should not hide the implementation of structure in C. Why? Because C is design to trust the user, if your user want to "play" with his data, you can't prevent it. C allow user to hack easily and this could improve the use of your library. For example, if your library don't have a code functionality the user could code it.

The second problem is that you hide the pointer in the typedef. You should not hide pointer, this made the code hard to read. And the user could think that this is not a pointer and try to write StringBuilder *foo;. A answer about this is available on stackoverflow.

For the following, I will assume that the user know that you hide the pointer and I will use explicit struct StringBuilder in the code.

Next I will talk about your function sb_new(), first I think you should not allocate the structure StringBuilder, this made a lot of overhead. You should separate your function into two sb_new() and sb_init(), one if the user want allocate the structure and one if the user take care of it.

Plus, you use calloc(), this add a lot of useless overhead. You should use malloc(). If the user want to affect the string by zero you should implement an other function for it, like sb_unused_zero().

And, if the user want a zero size string you send NULL, I don't see the point. Maybe you should use this value for tell to the function to use a default value like 42.

A detail about your call of malloc(), you could write struct StringBuilder *ret = malloc(sizeof *ret);, this is more readable and you can read this answer about it. You don't need to use sizeof char because by definition the is equal to 1.

Example:

struct StringBuilder *sb_new(size_t init_cap) {
    struct StringBuilder *sb = malloc(sizeof *sb);
    if (!sb) return NULL;
    if (!sb_init(sb, init_cap)) {
        free(sb);
        return NULL;
    }
    return sb;
}

bool sb_init(struct StringBuilder *sb, size_t init_cap) {
    init_cap = init_cap == 0 ? 42 : init_cap;
    sb-&gt;mem = malloc(init_cap);
    if (!sb-&gt;mem) return false;
    sb-&gt;count = 0;
    sb-&gt;cap = init_cap;
    return true;
}

void sb_unused(struct StringBuilder *sb, char c) {
    memset(sb-&gt;mem + to-&gt;count, c, sb-&gt;cap - sb-&gt;count);
}


This allow the user to do the following:

struct foo {
    struct StringBuilder sb;
};

int main(void) {
    struct foo foo;
    sb_init(&amp;foo.sb, 0);
}


Let talks about sb_append(), you don't check the potential overflow when you multiply by your LOAD_FACTOR, so you have an undefined behavior.

In this function, you affect new allocate byte to zero, this is again a big overhead that is useless in most of use cast. Again, the user should use the sb_unused() function that affect unused byte in your string. Plus, you suppose that your LOAD_FACTOR is equal to 2 in your use of memset().

bool sb_append(StringBuilder sb, char c) {
    if (sb-&gt;count + 1 == sb-&gt;cap) {
        if (SIZE_MAX / LOAD_FACTOR &lt; sb-&gt;cap) return false; // this could be improve
        size_t new_cap = sb-&gt;cap * LOAD_FACTOR;
        char *new_mem = realloc(sb-&gt;mem, new_cap);
        if (!new_mem) return false;
        sb-&gt;mem = new_mem;
        sb-&gt;cap = new_cap;
    }
    sb-&gt;mem[to-&gt;count++] = c;
    return true;
}


Your next function sb_as_string() suppose that mem is already a valid c-string, maybe you should verify that your string contain a nil terminate byte.

I almost finish, the function sb_free_copy() has a strange name, maybe rename it to something like sb_into_string(). And your implementation is strange you add yourself the nil terminate byte contrary to the function sb_as_string(), more strcpy() already do it. Whatever, you should use memcpy() because you already know the good size to copy this would be faster.

Just a detail in sb_free() you verify that sb-&gt;mem is not NULL but free() handle this, so this is useless and add overhead.

Finally, maybe your structure should be rename String because I think the user could use it to keep dynamic string and not to transform it into standard c-string?

The last thing is about your use about bool, this limit the detail about the error. Maybe you could replace it by an enum of just an int that will give you more flexibility about error report.