5

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

Roronoa Zoro
  • 1,013
  • 2
  • 15
  • 25

2 Answers2

8

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));
Mysticial
  • 464,885
  • 45
  • 335
  • 332
  • I'm sorry about the *operand switching*, actually my mistake was in the comment, I want to copy from pImage to pTestDump. However, when I added * sizeof(int) it still did not work... – Roronoa Zoro Nov 26 '11 at 04:27
  • Referring to the other comment you received, did you also mean `pPixel` to be `pImage`? If so, you could be printing out the wrong thing. – Mysticial Nov 26 '11 at 04:30
  • Oh sorry I meant pImage. I'm still having the same problem. My pointer pImage is pointing to a datatype *unsigned short*, could this be the problem? – Roronoa Zoro Nov 26 '11 at 04:36
  • That will definitely be a problem when you print it out. But it won't be all zeros in that case. Since you mentioned it being `unsigned short`, it leads me to think that you're trying to copy the values over. `int` and `short` are different datatypes and are of different sizes, so you can't use `memcpy()` to copy the values over. If `pImage` is an `unsigned short` array of `204800` elements, you will be overrunning it during the `memcpy()`. – Mysticial Nov 26 '11 at 04:41
  • I changed the data type of the array into *unsigned short* and it's working now. Thank you. I have a follow up question; is using arrays like I did (*unsigned short myArr[size]*) the best way to allocate continuous blocks of memory? – Roronoa Zoro Nov 26 '11 at 04:54
  • 2
    "Best" is subjective and situation-dependent. There's basically 3 main ways to allocate contiguous memory in C++. 1) An array like you are using now. 2) allocate with `new` or `malloc` (don't use `malloc` in C++). 3) Use the `vector` class. Each of these have their pros/cons. One thing though is that you want to avoid creating massive arrays as you are right now. (see this question: http://stackoverflow.com/questions/1847789/segmentation-fault-on-large-array-sizes) – Mysticial Nov 26 '11 at 05:00
4

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));
André Caron
  • 44,541
  • 12
  • 67
  • 125
  • Thanks. But my mistake was in the comment. I want to copy from pImage to pTestDump. I multiplied by sizeof(int) but I'm still having the same problem... – Roronoa Zoro Nov 26 '11 at 04:28