简体   繁体   English

在C中通过memcpy将矩阵复制到另一个矩阵中

[英]copy a Matrix into another by memcpy in C

I want to write a function to copy a two matrices into one matrix by use of memcpy. 我想编写一个函数,使用memcpy将两个矩阵复制到一个矩阵中。 It is like C = [A; 就像C = [A; B] in Matlab. B]在Matlab中。 Because, I want to use this function for all the type of data, which means integer and float matrices, I used void pointer. 因为,我想对所有类型的数据使用此函数,这意味着整数和浮点矩阵,所以我使用了void指针。

the code is: 代码是:

void Repmatrix(void *M3, void *M1, int R1, int C1, void *M2, int R2, int C2, size_t t)
{
    if (RC == 'R')
    {
        int i;
        for (i = 0; i < R1*C1; i++)
        {
            memcpy(M3+i, M1+i, t);

        }
        for (j = 0; j < R1*C1; j++)
        {
            memcpy(M3+i+j, M1+j, t);

        }
    }
}

Any help and suggestion is appreciated. 任何帮助和建议,表示赞赏。

To clarify it the inputs are: 为了澄清这一点,输入是:

M1 = [
1 2
3 4]
M2 = [
5 6
7 8]

the output should be: 输出应为:

M3 = [
1 2
3 4
5 6
7 8]

First, there are some simple index errors, probably because you copy-and-pasted the loops: The second loop should use M2 , C2 and R2 . 首先,存在一些简单的索引错误,可能是因为您复制并粘贴了循环:第二个循环应使用M2C2R2

Then, as others have pointed out, you cannot do arithmetic to void * . 然后,正如其他人指出的那样,您不能对void *进行算术运算。 Void pointers are just anonymous handles. 空指针只是匿名句柄。 If you want to do something with them, you should cast them to pointers to a value type. 如果要对它们执行某些操作,则应将它们强制转换为指向值类型的指针。 (GCC seems to allow arithmetic on void * and treats them as char * for that matter; it only warns in -pedantic mode.) (GCC似乎允许对void *进行算术运算,并将此作为char *对待;它仅在-pedantic模式下发出警告。)

Because you don't know the value type in your function, only its size, cast them to char * , because sizeof(char) == 1 . 因为您不知道函数中的值类型,只知道其大小,所以将它们sizeof(char) == 1char * ,因为sizeof(char) == 1 Then you can use your size t in pointer arithmetic like so: 然后,您可以在指针算术中使用大小t ,如下所示:

void Repmatrix(void *M3, 
    void *M1, int R1, int C1, 
    void *M2, int R2, int C2, size_t t)
{
    char *MM1 = M1;
    char *MM2 = M2;
    char *MM3 = M3;
    int i, j;

    for (i = 0; i < R1*C1; i++) {
        memcpy(MM3 + i * t, MM1 + i * t, t);
    }

    for (j = 0; j < R2*C2; j++) {
        memcpy(MM3 + (i + j) * t, MM2 + j * t, t);
    }
}

You don't actually need the loop, because the memory in the top and bottom halves is contiguous if you store your matrix in a row-major format: 实际上,您不需要循环,因为如果以行为主格式存储矩阵,则上半部分和下半部分的内存是连续的:

void Repmatrix(void *M3,
    void *M1, int R1, int C1,
    void *M2, int R2, int C2, size_t t)
{
    char *top = M3;
    char *btm = top + R1 * C1 * t;

    memcpy(top, M1, R1 * C1 * t);
    memcpy(btm, M2, R2 * C2 * t);
}

A more typesafe variant is to stick to double as matrix value and use double * pointers: 类型安全性更高的变体是坚持使用double作为矩阵值,并使用double *指针:

void Repmatrix(double *M3,
    double *M1, int R1, int C1,
    double *M2, int R2, int C2)
{
    memcpy(M3, M1, R1 * C1 * sizeof(*M3));
    memcpy(M3 + R1 * C1, M2, R2 * C2 * sizeof(*M3));
}

You'll still have to use sizeof , because memcpy works with void pointers, but you'll catch any occurrences of passing the wrong pointer type to Repmatrix . 您仍然必须使用sizeof ,因为memcpy与void指针一起使用,但是您会捕获将错误的指针类型传递给Repmatrix

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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