简体   繁体   English

如何动态分配结构的2D数组?

[英]How do I dynamically allocate a 2D array of structs?

The dimensions are unknown at compile time, so I'm trying to allocate a 2D array of structs dynamically. 在编译时维度是未知的,所以我试图动态分配2D结构数组。 The code compiles but I get a bad access when accessing an element. 代码编译但访问元素时访问不正确。

// The struct
typedef struct
{
    NSInteger numActors;
    Actor *a1;
    Actor *a2;
    Actor *a3;
    Actor *a4;
    Actor *a5;
} GridNode;

// In interface
GridNode **grid;

// In init
NSInteger nx = inFrame.size.width / blockSize;
NSInteger ny = inFrame.size.height / blockSize;
grid = malloc(sizeof(GridNode) * nx * ny);
grid[10][20].numActors = 3; // EXC_BAD_ACCESS

C has only 1D arrays, so 2D ones can be defined in two ways: C只有1D数组,因此可以用两种方式定义2D数组:

  1. As an array of arrays, like GridNode **grid , so to be accessed by grid[x][y] , but then you have to init each row separately (right, as yehnan managed to anwser first): 作为一个数组的数组,比如GridNode **grid ,所以要通过grid[x][y] ,但是你必须分别初始化每一行(正确,因为yehnan设法先anwser):

     grid=malloc(sizeof(GridNode*)*nx); for(int e=0;e<nx;e++) grid[e]=malloc(sizeof(GridNode)*ny); 
  2. As a 1D array with tricky indexing: 作为具有棘手索引的一维数组:

     grid=malloc(sizeof(GridNode)*nx*ny); grid[(10-1)*nx+20] //grid[10,20] 

Roughly the code should look like: 代码应该大致如下:

grid = (GridNode **) malloc(sizeof(GridNode *) * nx);
int i;
for(i = 0; i < nx; i++) {
    grid[i] = (GridNode *) malloc(sizeof(GridNode) * ny);
}

And remember to free them. 并记得释放他们。

A simpler alternative would be to create a single-dimension NSArray or NSMutableArray and use a little math to access the correct row and column: 一个更简单的替代方法是创建一个单维NSArrayNSMutableArray并使用一点数学来访问正确的行和列:

NSUInteger width  = 10;
NSUInteger height = 10;
NSUInteger size   = width * height;
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:size];

To insert a GridNode at row 5, column 3, you could do this: 要在第5行第3列插入GridNode ,您可以这样做:

NSUInteger index = (row - 1) * width + col;
[array insertObject:myNode atIndex:index];

To retrieve a node from row 2 column 6 you would do: 要从第2行第6列检索节点,您将执行以下操作:

NSUInteger index = (row - 1) * width + col;
[array objectAtIndex:index];

The memory in your computer is of a single dimension. 计算机中的内存是单一维度的。 The multi-dimensional addressing that we're all familiar with from C is really just syntactic sugar that performs a similar operation as I've shown above. 我们从C中熟悉的多维寻址实际上只是语法糖,执行与上面所示类似的操作。

The only caveat I can think of is that you will probably have to convert GridNode from a C structure to an Objective-C class for this to work. 我能想到的唯一警告是,您可能必须将GridNode从C结构转换为Objective-C类才能使其工作。

The concept behind your allocation is wrong. 你的分配背后的概念是错误的。

Assuming x is your second dimension ([20] in your example) and y is your first dimension ([10] in your example), the y elements are just pointers to arrays of x. 假设x是你的第二个维度(在你的例子中是[20])而y是你的第一个维度(在你的例子中是[10]),y元素只是指向x数组的指针。 So conceptually, you would do 从概念上讲,你会这样做

D'oh', yehnan beat me to it. D'oh',yehnan打败了我。

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

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