4 ms·
The typical strcat code you see in the wild: char buf[100]; buf[0] = '\0'; strcat(buf, "Hello"); strcat(buf, " "); strcat(buf, "World"); strcat(buf, "!\n"
by ambrop7 11y ago
The typical strcat code you see in the wild:
char buf[100];
buf[0] = '\0';
strcat(buf, "Hello");
strcat(buf, " ");
strcat(buf, "World");
strcat(buf, "!\n");
The inefficiency is that each time you do strcat, the existing characters of the string are iterated. So something like a common sequence of building a string by appending little pieces has quadratic inefficiency.
- halayli 11y agoThis is a C problem in general. You can create your own data structures and use str.buf + str.len.
- bmm6o 11y agoIt's unfortunate that strcat & friends don't return a pointer to the new end of the string, or a count of the characters copied. You could use those to make subsequent cats more efficient.