正确的方式来初始化一个多维数组的动态指针? [英] The correct way to initialize a dynamic pointer to a multidimensional array?

查看:110
本文介绍了正确的方式来初始化一个多维数组的动态指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将它们扩展到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];


  1. new int [4]; 调用运算符新函数()

  2. 整数。

  3. 返回对此内存的引用。

  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)
}

这里是试图描述





释放内存




  1. 对于一维数组,
    //需要使用delete []运算符,因为我们使用新的[]运算符
    delete [] p; //由p指向的空闲内存;


  2. 对于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];

  1. new int[4]; calls operator new function()
  2. allocates a memory for 4 integers.
  3. returns a reference to this memory.
  4. 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

  1. For one dimensional array, // need to use the delete[] operator because we used the new[] operator delete[] p; //free memory pointed by p;

  2. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆