I posted previously here, but based on the comments, I had the wrong idea. Here is my question clearly: when reading from a stream, with an unknown amount of bytes coming in, what is the best way to save the data in memory to use at a later point?
Here are some ideas I have had:
mmap
a non-conflicting region of memory, andmmap
some more as necessary (downside: possibly annoying to manage)- create a linked-list structure of buffers, filling each before allocating the next (possibly easy to manage, given I write the API)
- allocate (on the stack) an array of pointers to such buffers (say 100, allocated using
malloc
) and hope that you don't have more than (100 * buffer size) worth of data to read (the easiest but least robust solution)
My idea, that the other post heavily rejected was:
- mess with
sbrk
andbrk
and do my own contiguous heap (which could then conflict withmalloc
and anyone that usesmalloc
-- though I personally wouldn't mind this since I only use the APIs inunistd
for this application, and it is a learning project)
Is there a standard method (or even a specific function) for attacking this specific problem: an unknown amount of data needs to be read and saved in memory, how do you save it?