I want to reserve dynamic memory from a socket. The size of the response is variable so I want to use realloc to allocate the proper memory size.
This is the code snippet that takes care of it:
char *ptr = (char*)calloc(sizeof(char),1024);
char *ptr2 = (char*)calloc(sizeof(char),1024);
//printf("aaaaaaa %p bbbbbbbb",ptr);
int nDataLength;
int i = 0;
while ((nDataLength = recv(Socket, ptr, 1024, 0)) > 0){
if (i > 0){//prepare in case that the response is bigger than 1024 bytes
printf("Data len: %d\n", nDataLength);
printf("%p points toa %d len: %d\n", ptr, *ptr, ((1024 * i) + nDataLength));
ptr2 = (char*)realloc(ptr2,((1024*i)+nDataLength));
printf("%p pints toa %d\n", ptr2, *ptr2);
system("pause");
memcpy(ptr2, ptr, nDataLength);
}
memcpy(ptr2, ptr, nDataLength);
i++;
}
The problem is that the buffer that I receive from the socket is allways being memcpyed to the same address so I am overwriting the previous buffer. How can I move ptr2 to not overwrite the previous store buffer? It must be somethin like:
memcpy(ptr2+((1024 * i), ptr, nDataLength);
but it does not work.
Thank you