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 stackoverflow.
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 this answer about it. You don't need to use sizeof char
because by definition the is equal to 1
.
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.
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
.
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.
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
.
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 hisif the user take care of it.
Plus, you use calloc()
, this add a lot of useless overhead. You should use malloc()
, if you. If the user want to affect the string by zero you should implement an other function for it, like sb_unused_zero()sb_unused_zero()
.
And, if the user want a zero size string you send NULLNULL
, I don't see the point. Maybe you should use this value for tell to the function to use a default value like 42
.
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 his the user take care of it.
Plus, you use calloc()
, this add a lot of useless overhead. You should use malloc()
, if you 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 this value for tell to the function to use a default value like 42
.
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
.
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->mem = malloc(init_cap);
if (!sb->mem) return false;
sb->count = 0;
sb->cap = init_cap;
return true;
}
void sb_unused(struct StringBuilder *sb, char c) {
memset(sb->mem + to->count, c, sb->cap - sb->count);
}
A detail about your call of malloc()
, you could write struct StringBuilder *ret = malloc(*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
.
struct StringBuilder *sb_new(size_t init_cap) {
struct StringBuilder *sb = malloc(*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->mem = malloc(init_cap);
if (!sb->mem) return false;
sb->count = 0;
sb->cap = init_cap;
return true;
}
void sb_unused(struct StringBuilder *sb, char c) {
memset(sb->mem + to->count, c, sb->cap - sb->count);
}
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
.
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->mem = malloc(init_cap);
if (!sb->mem) return false;
sb->count = 0;
sb->cap = init_cap;
return true;
}
void sb_unused(struct StringBuilder *sb, char c) {
memset(sb->mem + to->count, c, sb->cap - sb->count);
}