Nice walkthrough. Keeping len and cap next to the pointer is what makes appends cheap and avoids strlen on every call. Have you thought about a small-string optimisation next, storing short strings inline in the struct?
Storing short strings inline is a cool idea!
I read about it in an article about C++ strings but, need to work on it.
Thank you so much for the idea! π
I like this. Though I do prefer the shorthand const char *str[] = "Hello"; Good job on making this though. I really like it and how simple but functional it is. Idea that you could add next is add the fmt, ... to the function for making and reading strings so you can add variables to strings.
s->s = NULL inside string_free is a small habit that pays off later. free the same string twice by accident and it's just free(NULL), nothing explodes π
@technogamerz and @hemapriya_kanagala
Sis, my first post that got many likes π
I want to say a special thanks to you two, because you were supporting me from the beginning!
πππ
Hey, I'm Hema π
Developer, writer, and creator of Dev Opportunity Radar, a weekly series published every Friday on DEV, helping people discover opportunities they might otherwise miss.
Hey, I'm Hema π
Developer, writer, and creator of Dev Opportunity Radar, a weekly series published every Friday on DEV, helping people discover opportunities they might otherwise miss.
I love the King Nyaomaru series! πΉπ
I barely know C, so I found it really interesting that you can build your own string abstraction like this.
Seeing the whole flow from allocating memory on the heap to manually freeing it made the idea much easier to understand.
Thanks for the clear explanation, and congrats on your first
Necozpost! πΈπThank you so much π
string_new("King Nyaomaru");π€£π€£π€£You're welcome, bro! To be honest, I understood the concept too. πβ€οΈ
Nice walkthrough. Keeping len and cap next to the pointer is what makes appends cheap and avoids strlen on every call. Have you thought about a small-string optimisation next, storing short strings inline in the struct?
Storing short strings inline is a cool idea!
I read about it in an article about C++ strings but, need to work on it.
Thank you so much for the idea! π
I like this. Though I do prefer the shorthand const char *str[] = "Hello"; Good job on making this though. I really like it and how simple but functional it is. Idea that you could add next is add the fmt, ... to the function for making and reading strings so you can add variables to strings.
Format function like sprintf for the string would be cool!
Thank you so much for the idea! π
s->s = NULL inside string_free is a small habit that pays off later. free the same string twice by accident and it's just free(NULL), nothing explodes π
True!
@technogamerz and @hemapriya_kanagala
Sis, my first post that got many likes π
I want to say a special thanks to you two, because you were supporting me from the beginning!
πππ
Congratulations! Seeing your happiness made my day, I'm so happy for you π₯°β€οΈ
so happy for you ra π Here's to many more likes and lots more love coming your way! You deserve it π₯³
You make even something like strings in C so interesting to read ra π Looking forward to the next posts π
Thanks akka π
Memory management in C, takes a lot of time, one small thing breaks everything.
Its fun and frustrating π