Reputation: 79
In my program, I have a global buffer string, say
char buffer[110];
It reads from an adapter and stores strings yet to be proceeded. I have something like this:
while(...){
readfromAdapter();
copytoBuffer();
processAndRemoveSomeCharsFromBuffer();
}
Every time a part of the string is proceeded, I want it removed from the buffer. I searched about how to remove first few characters from strings in C, in most answers I they increase the pointer, in this case buffer
. However, this should cause overflow in this case.
I have 2 methods in mind.
One is every time I use up the space in buffer, I free the old space and allocate some new space for it. In this case, I'll change buffer from an char array to global char pointer.
The other one is every time some chars are proceeded, I copy the old buffer+len
to buffer
.
How should I do it?
Upvotes: 2
Views: 1806
Reputation: 1193
Allocating and freeing memory dynamically are pretty expensive. And if the allocated space is big enough to invoke a SYSCALL, it'll cost you extra time. You should use the space you have if it's practical for your program, copying from a string to another is way better.
Upvotes: 1