The fastest way to expand a byte array?
Posted: Thu Jul 24, 2008 3:30 am
Hello
I'm writing a general purpose server in C , and I'm using a blocking function for receiving, so I read data byte-by-byte.
Anyway, you can see that I need to append these bytes (chars) to an array, so I can later read them/parse them/whatever I need.
A few logical answers (with even more questions following them) come to mind :
I can implement this as a linked list, but would it be wasteful on memory? Will it be fast, as it needs to be?
I can realloc() it, to add new bytes. That looks rather slow (unless I realloc() +10 bytes at a time)
All in all, it seems to me that realloc() is the way to go, after all, it is just malloc()+free() with some pointer swapping, which I would have to use in a linked list anyway. But, my server is a threaded beast, and (besides the main thread) I will have a thread dealing with every connection that my server accepts.
So, my question is :
Is there a faster way to do this?
Do you recommend using linked lists, in order to make my program more flexible (for instance, be able to receive more that 1 byte at a time)?
Should I simply use realloc() ?
I'm writing a general purpose server in C , and I'm using a blocking function for receiving, so I read data byte-by-byte.
Anyway, you can see that I need to append these bytes (chars) to an array, so I can later read them/parse them/whatever I need.
A few logical answers (with even more questions following them) come to mind :
I can implement this as a linked list, but would it be wasteful on memory? Will it be fast, as it needs to be?
I can realloc() it, to add new bytes. That looks rather slow (unless I realloc() +10 bytes at a time)
All in all, it seems to me that realloc() is the way to go, after all, it is just malloc()+free() with some pointer swapping, which I would have to use in a linked list anyway. But, my server is a threaded beast, and (besides the main thread) I will have a thread dealing with every connection that my server accepts.
So, my question is :
Is there a faster way to do this?
Do you recommend using linked lists, in order to make my program more flexible (for instance, be able to receive more that 1 byte at a time)?
Should I simply use realloc() ?