将动态指针初始化为多维数组的正确方法? [英] The correct way to initialize a dynamic pointer to a multidimensional array?
问题描述
当我将它们定义为2维和更高时,我一直在运用动态指针。例如,我想要一个指向2D数组的指针。我知道:
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] [4] ;
这样做有效,但这不是我想要完成的。通过这样做,我被限制为至少对任何后续维度使用常量值,但是我希望在运行时完全定义它,因此动态。
我如何去做这个多维指针?
让我们从一些基本的例子开始。 >
当你说 int * P = new int [4];
-
new int [4];
调用operator new function() - 为4个整数。
- 返回对此内存的引用。
-
要绑定此引用,您需要具有相同类型的指针的返回参考,所以你做
int * P = new int [4]; //当您创建一个整数
//的数组时,您应该将其分配给一个指针整数
对于多维数组,您需要分配一个指针数组,然后用数组的指针填充该数组,如下所示:
int ** p;
p = new int * [5]; (int i = 0; i <5; ++ i){
p [i] = new int [ 10];
//每个第i个指针现在指向动态数组(大小10)
//实际int值
}
这是它的样子:
释放内存
-
对于一维数组,
//需要使用delete []运算符因为我们使用新的[]运算符
delete [] p; // p $;
-
对于2d Array,
//需要使用delete []运算符,因为我们使用新的[]运算符
for(int i = 0; i< ; 5; ++ i){
delete [] p [i]; //删除整数的内部数组;
}
delete [] p; //删除指针保持指针数组;
避免内存泄漏和悬挂指针!
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??
Let's start 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 bind 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 an array of integer // you should assign it to a pointer-to-integer
For a multi-idimensional array, you need to allocate an array of pointers, then fill that array with pointers to arrays, like this:
int **p;
p = new int*[5]; // dynamic `array (size 5) of pointers to int`
for (int i = 0; i < 5; ++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 it looks like:
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 < 5; ++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屋!