That is what the std:list:assign function is for.
buffer.assign(buf, buf+bytes);
That will replace the existing contents though. If you want to add to the existing list then AFAIK C++ doesn't provide a way to do that for an array but a simple iteration loop works.
for (int index = 0; index < bytes; ++index)
buffer.push_back(buf[index]);
Note that insert
might work here with an array, never tried it. It has the same overload as assign
so you could pass the start and end of the array to it along with the position to insert (the end of the list) and see what happens.
As for whether you should be using std:list
or not though that is dependent on what you're trying to do. In terms of asking
if I do not use std::list I have to free char array and re-create them. right?
No you don't have to, but you might want to depending on what you're doing. If you want to allocate your buf
and the 3K size limit is sufficient for your message then you can use that single buffer to read data until you reach the limit. All you would need to do is call recv
again but pass the address of the element where you want the next batch of data to be inserted. Perhaps something like this, not tested.
char buf[3096]={0};
int bufferSize = sizeof(buf);
char* position = buf;
while(true) {
int bytes=recv(socket, buf, bufferSize);
position += bytes;
bufferSize -= bytes;
if (bufferSize <= 0) {
// Ran out of space, now what?
};
//Whatever logic to determine if you need to read again
}