I am new to objective-c and am doing something that works for the moment but am afraid might break someday.
我是Objective-c的新手,我正在做一些目前有效的事情,但恐怕有一天可能会破产。
In one of my object, I declare a matrix of integer as instance variable. As I do not know the size of the matrix yet, I declared it like this:
在我的一个对象中,我声明一个整数矩阵作为实例变量。由于我还不知道矩阵的大小,我宣称它是这样的:
int **_matrix;
Later in the code, when I know the amount of lines and columns of my matrix, I initialize it like this:
稍后在代码中,当我知道矩阵的行数和列数时,我将其初始化为:
_matrix = malloc(sizeof(*_matrix) * columns * lines);
for (i = 0; i < columns; i++) {
_matrix[i] = malloc(sizeof(int) * lines);
for(j = 0; j < lines; j++)
_matrix[i][j] = -1;
}
Assuming my allocation is correct (please, feel free to tell me if it is in fact wrong), can I confidently access the elements using the 2-dimension array syntax?
假设我的分配是正确的(请随时告诉我,如果它确实是错误的),我可以使用二维数组语法自信地访问元素吗?
I am asking because from what I read, when you create an actual 2-dimension array the memory is reserved continuously but that it might not be the case using double pointers. Therefore I am afraid that calling
我问,因为从我读到的,当你创建一个实际的二维数组时,内存是连续保留的,但可能不是使用双指针的情况。所以我害怕打电话
_matrix[i][j]
would actually point to a random place in the memory (and not the place I allocated).
实际上会指向内存中的随机位置(而不是我分配的位置)。
Thank you for your answers!
谢谢您的回答!
1 个解决方案
#1
1
Yes, it's safe. It is true that two-dimensional arrays on the stack are generally allocated contiguously, but you needn't worry about that. The important thing is that the brackets are evaluated one pair at a time, not as some special [][]
operator, and brackets dereference pointers. So it will dereference your first pointer, and then it will dereference the "row" pointer that was yielded by the first dereference.
是的,这很安全。确实,堆栈上的二维数组通常是连续分配的,但您不必担心这一点。重要的是括号一次被评估一对,而不是一些特殊的[] []运算符和括号解引用指针。因此它将取消引用您的第一个指针,然后它将取消引用由第一个取消引用产生的“行”指针。
More info: Array accesses and pointer dereferences are equivalent. The expression x[y]
is equivalent to *(x+y)
. So your code first dereferences the pointer value (matrix + i)
, which gives you another pointer value (since you had an array of pointers). After this, your code dereferences the pointer (matrix[i] + j)
and assigns it the value -1. As you can see, this is precisely what you want it to do.
更多信息:数组访问和指针解引用是等效的。表达式x [y]等于*(x + y)。所以你的代码首先取消引用指针值(矩阵+ i),它给你另一个指针值(因为你有一个指针数组)。在此之后,您的代码取消引用指针(matrix [i] + j)并为其赋值-1。如您所见,这正是您希望它做的。
#1
1
Yes, it's safe. It is true that two-dimensional arrays on the stack are generally allocated contiguously, but you needn't worry about that. The important thing is that the brackets are evaluated one pair at a time, not as some special [][]
operator, and brackets dereference pointers. So it will dereference your first pointer, and then it will dereference the "row" pointer that was yielded by the first dereference.
是的,这很安全。确实,堆栈上的二维数组通常是连续分配的,但您不必担心这一点。重要的是括号一次被评估一对,而不是一些特殊的[] []运算符和括号解引用指针。因此它将取消引用您的第一个指针,然后它将取消引用由第一个取消引用产生的“行”指针。
More info: Array accesses and pointer dereferences are equivalent. The expression x[y]
is equivalent to *(x+y)
. So your code first dereferences the pointer value (matrix + i)
, which gives you another pointer value (since you had an array of pointers). After this, your code dereferences the pointer (matrix[i] + j)
and assigns it the value -1. As you can see, this is precisely what you want it to do.
更多信息:数组访问和指针解引用是等效的。表达式x [y]等于*(x + y)。所以你的代码首先取消引用指针值(矩阵+ i),它给你另一个指针值(因为你有一个指针数组)。在此之后,您的代码取消引用指针(matrix [i] + j)并为其赋值-1。如您所见,这正是您希望它做的。