0

Hi all,

First I create a dynamic buffer and set all element to zero. Then add a int value first to the buffer. Then after that int value add a string to the buffer, actually at the end of int value. But seems my code overwrite each other.

Here is the code,

char* tmpBuffer = new char[10];// Dynamic allocation
	::memset(tmpBuffer, 0, 10);

	::memcpy(tmpBuffer, &iReq, 4);
	::memcpy(tmpBuffer, &strGetName, 6);

How can I avoid this issue.

4
Contributors
7
Replies
8
Views
9 Years
Discussion Span
Last Post by kia_barghi60
0

Perhaps if you viewed the syntax as

::memcpy(&tmpBuffer[0], &iReq, 4);
	::memcpy(&tmpBuffer[4], &strGetName, 6);
0

Thanks Salem, I've just think how to move the buffer directing pointer.. Thanks again.

One more question, to avoid effect of the past values I've clean the buffer as follows.

::memset(tmpBuffer, ' ', 10);

Is that correct, or should I use just zero.

0

Entirely unnecessary, as you're going to write 10 bytes anyway.
So whatever you do with the memset will be lost.

0

hi, all
I have a problem with my project in memcpy.

`memcpy' undeclared (first use this function)

although I write #include <string.h> in my main page.
what should I do?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.