Roronoa Zoro
Roronoa Zoro

Reputation: 1013

Copy blocks of memory to another part of memory

I'm trying to copy a continuous block of data from one location in the Main Memory to another location. Here's what I did so far, but it's not working. It seems that after applying 'memcpy', the content of my array 'testDump' becomes all zeros.

//Initialize array to store pixel values of a 640x480 image
int testDump[204800];   
for(int k = 0; k<204800; k++)
   testDump[k] = -9;

//pImage is a pointer to the first pixel of an image 
pImage = dmd.Data();

//pTestDump is a pointer to the first element in the array
int* pTestDump = testDump;

//copy content from pImage to pTestDump
memcpy (pTestDump, pImage, 204800);

for(int px_1 = 0; px_1<300; px_1++)
{
   std::cout<<"Add of pPixel: "<<pImage+px_1<<", content: "<<*(pImage+px_1);
   std::cout<<"Add of testDump: "<<pTestDump+px_1<<", content: "<<*(pTestDump+px_1);
}

Advice and suggestions are appreciated.

Thanks

Roronoa Zoro

Upvotes: 5

Views: 2309

Answers (2)

Andr&#233; Caron
Andr&#233; Caron

Reputation: 45284

It seems that after applying 'memcpy', the content of my array 'testDump' becomes all zeros.

//copy content from pTestDump to pImage
memcpy (pTestDump, pImage, 204800);

The arguments are reversed with respect to the comment. I think you meant the following.

//copy content from pTestDump to pImage
memcpy (pImage, pTestDump, 204800*sizeof(int));

Upvotes: 4

Mysticial
Mysticial

Reputation: 471499

The first problem I see is this:

memcpy (pTestDump, pImage, 204800);

should be this:

memcpy (pTestDump, pImage, 204800 * sizeof(int));

You forgot the sizeof(int) so you're only copying a part of the data.

The other problem is that you switched the order of the operands in memcpy(). The destination is the first operand:

memcpy (pImage, pTestDump, 204800 * sizeof(int));

Upvotes: 8

Related Questions