stclib / STC

A modern, user friendly, generic, type-safe and fast C99 container library: String, Vector, Sorted and Unordered Map and Set, Deque, Forward List, Smart Pointers, Bitset and Random numbers.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

behaviour of cstr_clear is suprising

polijan opened this issue · comments

I noticed that calling the cstr_clear function de-allocates the entire dynamic memory of a string. This behaviour is surprising: it functions like some sort of reset, not like the "clear" method from STL(*). If I have a loop in which I clear a string and dynamically re-append a lot stuff to it, then it will trigger many reallocations at every iteration of the loop which could seriously degrade performance.

If the behaviour differs from STL, perhaps the function should have another name (reset ?). In any case, clearing a string while keeping its capacity is also pretty useful, isn't it?

(*) C++ may actually allow to just reset a string, but no STL implementation makes clear() affect the capacity of a string.

You are right! Will change so that it just resets the size to 0.

Add: cstr_resize(&str, 0, 0) also does exactly this, as it should! There is a specific function cstr_shrink_to_fit() to minimize memory usage when needed.