This seems to be the main question, but I would ask him to clarify the situation, than to spend many more days on it. I am trying to copy data to the buffer that I receive (recv call), which will then be transferred to the file, I want to use memcpy to continuously add / add data to the buffer until the buffer size is sufficient to store more data, than me than using realloc. The code is as follows.
int vl_packetSize = PAD_SIZE + (int)p_size - 1; // PAD_SIZE is the size of char array sent //p_size is the size of data to be recv. Same data size is used by send int p_currentSize = MAX_PROTO_BUFFER_SIZE; int vl_newPacketSize = p_currentSize; char *vl_data = (char *)malloc(vl_packetSize); memset((char *)vl_data,'\0',vl_packetSize); /* Allocate memory to the buffer */ vlBuffer = (char *)malloc(p_currentSize); memset((char *)vlBuffer,'\0',p_currentSize); char *vlBufferCopy = vlBuffer; if(vlBuffer==NULL) return ERR_NO_MEM; /* The sender first sends a padding data of size PAD_SIZE followed by actual data. I want to ignore the pad hence do vl_data+PAD_SIZE on memcpy */ if((p_currentSize - vl_llLen) < (vl_packetSize-PAD_SIZE)){ vl_newPacketSize +=vl_newPacketSize; char *vlTempBuffer = (char *)realloc(vlBufferCopy,(size_t)vl_newPacketSize); if(vlTempBuffer == NULL){ if(debug > 1) fprintf(stdout,"Realloc failed:%s...Control Thread\n\n",fn_strerror_r(errno,err_buff)); free((void *)vlBufferCopy); free((void *)vl_data); return ERR_NO_MEM; } vlBufferCopy = vlTempBuffer; vl_bytesIns = vl_llLen; vl_llLen = 0; vlBuffer = vlBufferCopy+vl_bytesIns; fprintf(stdout,"Buffer val after realloc:%s\n\n",vlBufferCopy); } memcpy(vlBuffer,vl_data+PAD_SIZE,vl_packetSize-PAD_SIZE); /* fprintf(stdout,"Buffer val before increment:%s\n\n",vlBuffer); fprintf(stdout,"vl_data length:%d\n\n",strlen(vl_data+PAD_SIZE)); fprintf(stdout,"vlBuffer length:%d\n\n",strlen(vlBuffer)); */ vlBuffer+=(vl_packetSize-PAD_SIZE); vl_llLen += (vl_packetSize-PAD_SIZE); vl_ifNotFlush = 1; //fprintf(stdout,"Buffer val just before realloc:%s\n\n",vlBufferCopy); }
Problem: when will I transfer data to a file later. Only the first recv / data added to the buffer gets into the file. Also, when I print the vlBufferCopy value (which indicates the first location of the data returned by malloc or realloc), I get the same result. If I reduce the size by 1, I see the whole data in the file, but it somehow skips the new line symbol, and therefore the data is not inserted into the corresponding format in the file. I know this is due to the termination of '\ 0', but some of them reduce the size by 1
(vlBuffer+=(vl_packetSize-PAD_SIZE-1);)
skips the newline character. fputs when placing data removes the trailing null character Please let me know what I am missing here to check or in logic (Note: I tried using strcat:
strcat(vlBuffer,vl_data+PAD_SIZE);
but I wanted to use memcpy as it is faster and also it can be used for any type of buffer, not just a character pointer
thanks