Working with strings in C

Hi!

I'm working on a small program to keep track of my economy. I can get excerpts from my banks web portal of all transactions in the format
1
Date,Receiver,Amount

I'm using SDL to kickstart myself and I'm trying to create a nice table to list each transaction. I was thinking something like tab separated listing with one line per transaction. It would involve concatenating each parsed value with a custom tab (since it seems SDL_TTF doesn't do whitespace). This led me out on the internet where I stumbled upon this article which made me groan a little.
It seems that strcat is super inefficient and so is malloc when used a lot. Some other techniques are proposed both for concatenation and handling of strings. So I have actually two questions (thanks to that article):
1. I feel like someone should have solved this problem of working with string already. Is there a library or something I can use for this?
2. I'll probably be allocating and reallocating a bunch, will that be a problem?

Just a bit confused. Thought I had this down, but the more you know..
Thanks!

/Robert
The biggest point he's moaning about is that null termination requires a loop to find out how long your string is. And knowing how long your string is is required for most operations on it. There is also that current cpus are better at working with 4 bytes (or even 16 bytes when doing simd) at a time rather than 1 at a time.

If instead you have a string struct with a char* and a size_t field for the data+length then you don't need that loop.

As for malloc, a general purpose allocator is not going to be optimal for your use case. It never will be. Instead you can create your own custom allocator based on the allocation pattern you will have and give it a big buffer to throw around.
Thanks!
I was actually thinking about having a string struct that keeps track for the string length, nice to see I was on the right track. As for memory allocation, sounds a bit advanced for me. I guess malloc will have to do until it becomes a problem (if it ever does for my small app).

/Robert
Raybolio
Thanks!
I was actually thinking about having a string struct that keeps track for the string length, nice to see I was on the right track. As for memory allocation, sounds a bit advanced for me. I guess malloc will have to do until it becomes a problem (if it ever does for my small app).

/Robert


Nearly every language that isn't C uses some kind of pointer+size struct for strings, even C++'s std::string.

I call null terminated strings one of the bigger mistakes in C and we are stuck with it until the OS system functions stop requiring them in their APIs.
I suggest you take a look at Ginger Bill's string library as a starting point.
Or just use that and be done.

https://github.com/gingerBill/gb/blob/master/gb_string.h