正确的方式来初始化一个多维数组的动态指针? [英] The correct way to initialize a dynamic pointer to a multidimensional array?
问题描述
当我将它们扩展到2个维度和更高的时候,我一直在使用动态指针。例如我想要一个指向二维数组的指针。我知道:
int A [3] [4];
int(* P)[4] = A;
完全合法(即使我不完全明白为什么)。考虑到:
int * P = new int [4]
有效,我想象:
int ** P = new int [5] [7];
也可以工作,但不是。此代码说明错误:
错误:类型(*)[7]的值不能用于初始化实体
类型int **
通过看到这个新部分成为一个指针我创建了一个7个整数的数组:
int(* P)[4] = new int [7] ;
这样可以工作,但这不是我想要完成的。通过这样做,我被限制为至少为任何后续维度使用一个常量值,但我希望它在运行时被完全定义,因此是动态的。
让我们从一些基本的例子开始
当你说 int * P = new int [4];
-
new int [4];
调用运算符新函数() - 整数。
- 返回对此内存的引用。
- 要捕获此引用,您需要具有与返回引用相同类型的指针, do
int * P = new int [4];
//你创建的数组的整数
你应该抓住它整数指针
添加在 - :
但是当你想创建一个多维数组时,应该这样做:
int * * p;
p = new int * [4]; (int i = 0; i <4; ++ i){
p [i] = new int [int]指向int类型的指针的动态数组10];
//每个第i个指针现在指向实际int值的动态数组(大小10)
}
这里是试图描述
释放内存
-
对于一维数组,
//需要使用delete []运算符,因为我们使用新的[]运算符
delete [] p; //由p指向的空闲内存;
-
对于2d数组,
//需要使用delete [因为我们使用新的[]运算符
for(int i = 0; i <4; ++ i){
delete [] p [i]删除整数的内部数组;
}
delete [] p; // delete pointer holding array of pointers;
避免<悬挂指针!
I've been having bad luck with with dynamic pointers when I range them to 2 dimensions and higher. For example I want a pointer to a 2D array. I know that:
int A[3][4];
int (*P)[4] = A;
Is completely legit (even if I don't completely understand why). Taking into consideration that:
int *P = new int[4];
works, I imagined that:
int **P = new int[5][7];
Would also work, but it's not. This code states the error:
Error: A value of type "(*)[7]" cannot be used to initialize an entity of
type "int **"
By seeing this the new part becomes a pointer to an array of 7 integers I made:
int (*P)[4] = new int[7][4];
And this does work but it's not what I want to accomplish. By doing it like that I'm limited to at least using a constant value for any subsequent dimension, but I want it to be fully defined at run time and therefore "dynamic".
How could I go and make this multidimensional pointer work??
Lets starts with some basic examples
When you say int *P = new int[4];
new int[4];
calls operator new function()- allocates a memory for 4 integers.
- returns a reference to this memory.
- to catch this reference you need to have same type of pointer as that of return reference so you do
int *P = new int[4];
//as you created array of integer
you should catch the it integer pointer
Add On -:
But when you want to create a multidimensional array you should do,
int **p;
p = new int*[4]; // dynamic `array (size 4) of pointers to int`
for (int i = 0; i < 4; ++i) {
p[i] = new int[10];
// each i-th pointer is now pointing to dynamic array (size 10) of actual int values
}
Here is what am trying to depict
To free the memory
For one dimensional array, // need to use the delete[] operator because we used the new[] operator
delete[] p; //free memory pointed by p;
For 2d Array, // need to use the delete[] operator because we used the new[] operator
for(int i = 0; i < 4; ++i){ delete[] p[i];//deletes an inner array of integer; }
delete[] p; //delete pointer holding array of pointers;
Avoid memory leakage and dangling pointers!
这篇关于正确的方式来初始化一个多维数组的动态指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!