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.

Recommended Answers

All 7 Replies

Why not just add two int's and let the compiler perform the crap you don't have to?

Perhaps if you viewed the syntax as

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

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.

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

Ok, thanks. But if I use it what happened. I mean use of a space.

Ok, I got the point 0 and '0' has two different meaning. ASCII.

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.