Code:
char* data = NULL;
data = new char[lengthOfParam]; //lengthOfParam = 3
//after allocation **data = ¥¥¥¥Ü\r**
memcpy(data,&buffer[offset],lengthOfParam); //**data = pki¥Ü\r**
Why i am getting that junk values??? How to avoid or remove those extra values bcs if i try to assign that value to any other array
ex:
obj[1] = data;
then the whole value with junk’ll be assigned to that variable.
Strings in C need to be NUL terminated. This means you need to add a zero value byte to the end of the string to indicate the end of the string. Because you have no indication of the end of the string when you view/print the value you are reading on past the end of your array into whatever memory is after it.
If the source data contains a NUL terminator you can simply allocate and copy 1 more byte, but assuming it is a fixed length field with no NUL termination you will need to manually add one:
Also further more looking at this line you posted:
I maybe wrong here and sorry if I am but I strongly suspect this line is not doing what you think it is. This will store a pointer to your string in
obj[1]not copy the data from your string. Hence if you deletedata,obj[1]would no longer be valid either.