如何使用 Memcpy() 函数
我想最后使用memcpy,而不是
block_orig_left[i1][j1]=block_orig[i1][j1];
pred_orig_left [i1][j1]=block_pred[i1][j1];
使用memcpy时出现错误
src/coder.c:909: 错误:二进制 * 的操作数无效(具有“unsigned int”和“int **”)
src/coder.c:910:错误:二进制 * 的操作数无效(具有“unsigned int”和“int **”)
int **block_orig_left=NULL;
block_orig_left=intmatrix(BSIZE_Y_LEVEL[levelv], BSIZE_X_LEVEL[levelv]);
pred_orig_left=intmatrix(BSIZE_Y_LEVEL[levelv], BSIZE_X_LEVEL[levelv]);
for(i1=0; i1<BSIZE_Y_LEVEL[levelv]; i1++)
for(j1=0; j1<BSIZE_X_LEVEL[levelv]; j1++)
{
block_orig_left[i1][j1]=block_orig[i1][j1];
pred_orig_left[i1][j1]=block_pred[i1][j1];
Average_block_orig_left+=block_orig[i1][j1];
}
memcpy(block_orig_left, block_orig, sizeof(int **)*block_orig);
memcpy(pred_orig_left, block_pred, sizeof(int **)*block_pred);
如何正确使用 memcpy?
I want to use the memcpy in the end, instead of
block_orig_left[i1][j1]=block_orig[i1][j1];
pred_orig_left [i1][j1]=block_pred[i1][j1];
I have an error using memcpy
src/coder.c:909: error: invalid operands to binary * (have ‘unsigned int’ and ‘int **’)
src/coder.c:910: error: invalid operands to binary * (have ‘unsigned int’ and ‘int **’)
int **block_orig_left=NULL;
block_orig_left=intmatrix(BSIZE_Y_LEVEL[levelv], BSIZE_X_LEVEL[levelv]);
pred_orig_left=intmatrix(BSIZE_Y_LEVEL[levelv], BSIZE_X_LEVEL[levelv]);
for(i1=0; i1<BSIZE_Y_LEVEL[levelv]; i1++)
for(j1=0; j1<BSIZE_X_LEVEL[levelv]; j1++)
{
block_orig_left[i1][j1]=block_orig[i1][j1];
pred_orig_left[i1][j1]=block_pred[i1][j1];
Average_block_orig_left+=block_orig[i1][j1];
}
memcpy(block_orig_left, block_orig, sizeof(int **)*block_orig);
memcpy(pred_orig_left, block_pred, sizeof(int **)*block_pred);
How do I use the memcpy correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我假设 block_orig、block_pred、block_orig_left 和 pred_orig_left 均声明为
int**
。您的代码中仅显示其中之一。您收到的错误出现在 memcpy 的参数
sizeof(int **)*block_orig
中。您正在尝试将整数 (sizeof(int**)
) 与 int** 类型的变量相乘。编译器无法理解该乘法。您需要将长度参数修复为 memcpy,但这仍然无法按预期工作。
int**
是指向整数数组的指针数组。如果您尝试 memcpy int**,您最终将覆盖外部数组。因此,我认为你需要一个循环并复制内部数组。这应该有效。
I'm assuming that block_orig, block_pred, block_orig_left, and pred_orig_left are all declared as
int**
. Only one of them is shown in your code.The error that you're getting is in the parameter to memcpy,
sizeof(int **)*block_orig
. You're trying to multiply an integer (sizeof(int**)
) with a variable of type int**. The compiler can't make sense of that multiplication.You need to fix your length parameter to memcpy, but that still won't work as desired.
An
int**
is an array of pointers to arrays of integers. If you tried to memcpy the int**, you'll end up overwriting the outer array. Therefore, I think you need a loop and copy the inner arrays.This should work.
您将 int** 的大小乘以 int** ,这是没有意义的。换句话说,如果你想知道一辆卡车上所有汽车的重量,你不能将“一辆车的重量”乘以“卡车”。您必须将 1 辆汽车的重量乘以卡车上的汽车数量。
memcpy 的第三个参数是要复制的字节数。您正确地获得了 int* 的大小,但是您想将其乘以结构中 int* 的数量。因此,如果我正确理解您的代码,您会想要使用,
因为您正在复制的结构似乎包含许多 int 双指针。
编辑:看看 David Yaw 的答案,我意识到他是正确的。我未能解决这样一个事实:内部指针可能不是一次性全部分配的,而是在 for 循环或其他东西中分配的,因此需要以类似的方式复制它们。我上面的方法会复制正确的内存量,但不一定是正确的内存。
You are multiplying the size of an int** by an int**, which doesn't make sense. In other words, if you wanted to know the weight of all the cars on a truck, you can't multiply "weight of 1 car" by "truck". You have to multiply the weight of 1 car by the number of cars on the truck.
The third parameter to memcpy is the number of bytes you want to copy. You are correctly getting the size of an int*, but then you want to multiply that by the number of int* in the structure. So, if I'm understanding your code correctly, you would want to use
since the structures you are copying seem to contain that many int double pointers.
EDIT: Looking at David Yaw's answer, I realize that he is correct. I failed to address the fact that the inner pointers were likely not all allocated at once, but rather in a for loop or something, so they would need to be copied in like manner. My way above would copy the right amount of memory, but it would not necessarily be the correct memory.
sizeof(int **)*block_orig
是大小与指针的乘积。我想您知道乘以指针没有意义,也不可能,正如您从编译器错误中看到的那样。我不知道
block_orig
或您的其他(顺便说一句,您听说过自描述变量名吗?)变量是什么意思,但是memcpy
将目标、源和字节大小作为参数。在整数矩阵的情况下,如果目标内存是连续的(即二维数组),则类似
sizeof(int) * numberOfElementsToCopy
的内容是有意义的。sizeof(int **)*block_orig
is the multiplication of a size with a pointer. I guess you know that multiplying a pointer doesn't make sense nor is it possible as you can see from the compiler error.I don't know what
block_orig
or your other (btw did you hear about self-describing variable names?) variables mean, butmemcpy
takes the target, the source, and the size in bytes as arguments.In your integer-matrix case, something like
sizeof(int) * numberOfElementsToCopy
would make sense, if the target memory is continuous (i.e. 2D array).