0
What is the correct way to implement dynamic strings?
I've been trying to figure out how to make dynamic strings and thought of this set of functions. But when I tried using them in a small program it worked fine for a while and then for some reason extendDynamicString started failing. I'm assuming that it has to do with realloc and that I'm misunderstanding something about its usage. I can't for the life of me figure out the solution. Any help is appreciated. this is just an example program not the one that failed https://code.sololearn.com/c5gTPuF6hm26/?ref=app
3 Answers
+ 2
The only changes I'd make is put '\0' into the string during initializeDynamicString and allocate in bigger chunks. My guess is you are fragmenting memory so you run out of areas big enough to use. Both the original and new locations must exist so the contents can be copied. If you allocate other things in memory, it will add to the problem.
+ 1
Say you have 10 locations 0 to 9. If 2 thru 4 is used for one pointer and 7 thru 9 is used for a second, 0, 1, 5, & 6 are still available. Therefore, you have 4 locations available, but the largest space is only 2 in size. This is fragmentation so it is likely realloc would fail.
0
Never even heard of fragmenting memory so I'm gonna have to look it up.
Thanks for the answer.