In c-strings we need to allocate reasonable size of memory. To avoid reallocations in string operations, we can use something like Stringbuilder in C# or Java or – in C – just allocate more memory for string. But still it can be a problem if we don’t know the memory requirement in advance. Do we have some implemetation like linked list? I mean to allocate list of blocks of memory and method c_str() which creates c-string from its nodes
liststring a(4); // requested block size
a.append("hello ");
a.append("world");
// should create three nodes, 4 bytes allocated for each
// "hell" -> "o wo" -> "rld"
a.c_str(); // "hello world";
Or do we use another approach if we want to avoid reallocations? Please explain if it is bad idea.
See the article on Ropes for a data structure that keeps strings as trees. It is similar to your idea.