realloc()ing memory for a buffer used in recv()

Posted by Hristo on Stack Overflow See other posts from Stack Overflow or by Hristo
Published on 2010-04-21T01:16:09Z Indexed on 2010/04/21 1:23 UTC
Read the original article Hit count: 336

Filed under:
|
|

I need to recv() data from a socket and store it into a buffer, but I need to make sure get all of the data so I have things in a loop. So to makes sure I don't run out of room in my buffer, I'm trying to use realloc to resize the memory allocated to the buffer. So far I have:

// receive response
int i = 0;
int amntRecvd = 0;
char *pageContentBuffer = (char*) malloc(4096 * sizeof(char));
while ((amntRecvd = recv(proxySocketFD, pageContentBuffer + i, 4096, 0)) > 0) {
    i += amntRecvd;
    realloc(pageContentBuffer, 4096 + sizeof(pageContentBuffer));
}

However, this doesn't seem to be working properly since Valgrind is complaining "valgrind: the 'impossible' happened:". Any advice as to how this should be done properly?

Thanks, Hristo

© Stack Overflow or respective owner

Related posts about realloc

Related posts about c