简体   繁体   English

指针指针上的 Memset - 为什么它不起作用?

[英]Memset on pointer of pointers - why won't it work?

I'm implementing a matrix library in c.我正在用 c 实现一个矩阵库。 I've already made a vector library and I have defined a matrix to be nothing but a collection of pointers pointing to vectors (so each pointer references a vector struct which is the matrices column.) I have a pointer of pointers instead of an array of pointers because a) I want jagged matrices to be possible b) I want vector operations to work on the individual columns of the matrix and c) I want the actual matrix to be dynamic.我已经创建了一个向量库,并且我已经定义了一个矩阵,它只是一个指向向量的指针集合(所以每个指针都引用一个向量结构,它是矩阵列。)我有一个指针指针而不是一个数组指针,因为 a) 我希望锯齿状矩阵成为可能 b) 我希望向量运算在矩阵的各个列上工作 c) 我希望实际矩阵是动态的。

Here is the matrix type definition:这是矩阵类型定义:

    typedef struct mat {
    size_t alloc; // num of allocated bytes
    size_t w, h; // dimensions for the matrix
    vec** cols; // each column is a vector
    } mat;

Suppose I want to resize the dimensions of the matrix.假设我想调整矩阵的维度。 The following code works just fine for me:以下代码对我来说很好用:

    void resizem(mat* m, size_t w, size_t h) {
    m -> alloc = w * VECTOR_SIZE;
    m -> cols = realloc(m -> cols, m -> alloc);
//    if(w > m -> w) {
//        memset(m -> cols + m -> w, init_vec(h), (w - (m -> w)) * VECTOR_SIZE);
//    }
    if(w > m -> w) {
        for(int i = m -> w; i < w; i++) {
            m -> cols[i] = init_vec(h);
        };
    }
    for(int i = 0; i < w; i++) {
        resizev(m -> cols[i], h);
    };
    m -> w = w;
    m -> h = h;
}

My approach was as follows: 1) re-compute the amount of bytes to reallocate and store this in the matrix struct (amount of columns * column size) 2) reallocate memory to the columns 3) if the matrix 'grew' in width then each new column needs to be initialised to a default vector.我的方法如下:1)重新计算要重新分配的字节数并将其存储在矩阵结构中(列数*列大小)2)将内存重新分配给列3)如果矩阵的宽度“增长”,则每个新列都需要初始化为默认向量。 4) resize the size of each vector in the matrix. 4) 调整矩阵中每个向量的大小。

Note the commented out lines however.但是请注意注释掉的行。 Why can't I just add an offset (the size of the former matrix) to the column pointers and use memset on the remaining columns to initialise them to a default vector?为什么我不能向列指针添加偏移量(前矩阵的大小)并在其余列上使用 memset 将它们初始化为默认向量? When I run this it doesn't work so I use a for loop instead.当我运行它时它不起作用,所以我改用 for 循环。

Note that if it helps at all here is the github link to the vector library so far: Github link请注意,如果它有帮助,这里是到目前为止矢量​​库的 github 链接: Github 链接

When you use memset() you only call initvec() once.当您使用memset()您只调用initvec()一次。 So all the elements would point to the same vector.所以所有元素都指向同一个向量。

Also, memset() assigns the value to each byte in the array.此外, memset()将值分配给数组中的每个字节。 But your row is supposed to contain pointers, not bytes.但是您的行应该包含指针,而不是字节。

BTW, your code is leaking lots of memory, because you never free() the old pointers in the row.顺便说一句,您的代码泄漏了大量内存,因为您从未free()行中的旧指针。 You need to do this before calling realloc() .您需要在调用realloc()之前执行此操作。

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

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