C语言中的指针指向一个结构体内部未分配的int(*)[row]类型的动态数组。

huangapple go评论58阅读模式
英文:

C Pointer to a dynamic array of type int(*)[row] inside where row is unalocated inside of struct

问题

以下是您要翻译的内容:

基本上,我想要有一个结构体,可以在其中放置数据和一个数组。

我知道使用int**然后malloc一个指针数组的方法,然后在每个指针上进行malloc。

但这不是我想要的。原因是从技术上讲,这会更慢,因为(某种原因,缓存方面的问题)。

此外,它允许数据不被分散。

如果我成功运行它,我将拥有创建动态2D数组并可以放置在结构体内的最佳方法。

以下是几乎完成的代码示例。

我知道这与已经提出的问题相似,甚至与我早期提出的问题相似,但我要求的是不同的答案。

(创建一个可以放置在结构体内的任意大小(行、列)2D数组的最佳可能方法(在堆上,使用动态内存分配malloc...),以便该数组是可以连续存储在内存中的数据类型。如果数据可以连续存储在内存中,则加分。)

#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int  row;
    int  col;
    int *(data)[];
} iMAT;

iMAT* create_matrix(int row, int col) {
    iMAT* matrix = malloc(sizeof(iMAT));    
    int (*arr)[row] = malloc(sizeof(int[col][row]));    

    matrix->row = row;
    matrix->col = col;
    matrix->data = arr;

    return matrix; 
}

int main() { 
        int row = 5;
        int col = 5;
        iMAT* matrix = create_matrix(row, col);

        return 0;
}

请注意,我已将代码翻译为中文,如您所要求。

英文:

Basically, I want to have a struct where I can put data and an array.

I know the method of using int** and then malloc an array of pointer.
then malloc at each pointer.

But that's not what I want. The reasons is that technically, this would be slower because (something something caching).

Also, it allows the data to not be fragmented.

And if I get it running, I will have the definitive best way to create a dynamic 2D array that can be placed inside a struct.

Here is an example of the nearly finished code.

I know this is similar to already asked question, even similar to a question I asked early on, but what I'm asking for is a different answer.

(What is the best possible way to create a 2d array of arbirtrary size (row, col) on the heap (dynamic memory, malloc...), such that this array is a data type that can be contained within a struct. Bonus point if the data can be stored consequitively on the memory.)

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

typedef struct {
    int  row;
    int  col;
    int *(data)[];
} iMAT;

iMAT* create_matrix(int row, int col) {
    iMAT* matrix = malloc(sizeof(iMAT));    
    int (*arr)[row] = malloc(sizeof(int[col][row]));    

    matrix-&gt;row = row;
    matrix-&gt;col = col;
    matrix-&gt;data = arr;

    return matrix; 
}

int main() { 
        int row = 5;
        int col = 5;
        iMAT* matrix = create_matrix(row, col);

        return 0;
}

答案1

得分: 2

这不会编译,因为这个结构体成员:

int *(data)[];

是一个指针数组,意味着这个赋值语句:

matrix->data = arr;

试图赋值给一个数组,这是不允许的。你想要的是一个指向未指定大小数组的指针:

int (*data)[];

然而请注意,由于指向的类型是不完整的,你将无法直接访问数组成员。要读取或写入数组成员,你需要将其强制转换为正确的类型:

((int (*)[matrix->cols])matrix->data)[1][2] = 4;

或者将其分配给正确类型的临时变量并使用该变量:

data[1][2] = 4;```

<details>
<summary>英文:</summary>

This doesn&#39;t compile because this struct member:

    int *(data)[];

Is an array of pointers, meaning that this:

    matrix-&gt;data = arr;

Is an assignment to an array which is not allowed.  What you wanted was a pointer to an array of unspecified size:

    int (*data)[];

Note however that you won&#39;t be able to access the array members directly because the pointed-to type is incomplete.  To read or write members of the array, you&#39;d either need to cast it to the proper type:

    ((int (*)[matrix-&gt;cols])matrix-&gt;data)[1][2] = 4;

Or assign it to a temporary of the proper type and use that:

    int (*data)[matrix-&gt;col] = matrix-&gt;data;
    data[1][2] = 4;


</details>



huangapple
  • 本文由 发表于 2023年4月16日 23:09:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76028561.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定