简体   繁体   中英

How to use Memcpy() function

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?

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.

// Still won't work.
memcpy(block_orig_left, block_orig, sizeof(int) * BSIZE_Y_LEVEL[levelv] * BSIZE_X_LEVEL[levelv]);

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.

for(int i = 0; i < BSIZE_Y_LEVEL[levelv]; i++)
{
    memcpy(block_orig_left[i], block_orig[i], sizeof(int) * BSIZE_X_LEVEL[levelv]);
}

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

sizeof(int**) * BSIZE_Y_LEVEL[levelv] * BSIZE_X_LEVEL[levelv]

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.

memcpy(block_orig_left, block_orig, sizeof(int **)*block_orig);

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, but memcpy 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 (ie 2D array).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM