Originally Posted by
Salem
> typedef String char;
> String* String_new(){
> String *self=calloc(1, sizeof(char));
You create a typedef, then don't use it.
If you change the type away from char, then calloc will still allocate a char. At that point, the code is broke.
> self=realloc(self, sizeof(char)*len+2);
1. If realloc fails, it returns NULL. This trashes the original pointer. If that's your only copy of the pointer, you have a memory leak.
2. realloc can MOVE the memory. Which in this case is a problem for you, since the caller doesn't get to find out about the change. So the next time you call this function, you're passing the OLD value of self again, which realloc will try to free (and hence, this is your double free).