2011-11-26 83 views
5

我试图将连续的数据块从主内存中的一个位置复制到另一个位置。这是我迄今为止所做的,但它不起作用。看起来,应用'memcpy'后,我的数组'testDump'的内容全部为零。将内存块复制到内存的另一部分

//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); 
} 

意见和建议表示赞赏。

感谢

罗罗亚·索隆

+0

什么是'pPixel'?你的意思是'pImage'吗? – Blastfurnace

+0

是的。我的意思是pImage。抱歉。我仍然有同样的问题... –

回答

8

我看到的第一个问题是这样的:

memcpy (pTestDump, pImage, 204800); 

应该是这样的:

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

你忘了sizeof(int)所以你只能是复制一部分数据。

另一个问题是,您切换memcpy()中操作数的顺序。目的地是第一个操作数:

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

我很抱歉*操作数切换*,其实我的错误是在评论中,我想从pImage复制到pTestDump。然而,当我添加* sizeof(int)它仍然没有工作... –

+0

参考你收到的其他评论,你是否也指'pPixel'是'pImage'?如果是这样,你可能会打印出错误的东西。 – Mysticial

+0

对不起,我的意思是pImage。我仍然有同样的问题。我的指针pImage指向一个数据类型* unsigned short *,这可能是问题吗? –

4

看来,应用“的memcpy”后,我的阵“testDump”的内容成为全零。

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

的参数反转相对于注释。我认为你的意思是以下。

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

谢谢。但我的错误在于评论。我想从pImage复制到pTestDump。我乘以sizeof(int),但我仍然有同样的问题... –

相关问题