From:https://stackoverflow.com/questions/18273370/the-correct-way-to-initialize-a-dynamic-pointer-to-a-multidimensional-array

Let's start 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 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

  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 < 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!

The correct way to initialize a dynamic pointer to a multidimensional array的更多相关文章

  1. C lang:Pointer and multidimensional array

    Xx_Introduction Double indrection:Address of Address;Pointer of Pointer Ax_Code #include<stdio.h& ...

  2. Linux Programe/Dynamic Shared Library Entry/Exit Point && Glibc Entry Point/Function

    目录 . 引言 . C/C++运行库 . 静态Glibc && 可执行文件 入口/终止函数 . 动态Glibc && 可执行文件 入口/终止函数 . 静态Glibc & ...

  3. Dynamic Signals and Slots

    Ref https://doc.qt.io/archives/qq/qq16-dynamicqobject.html Trolltech | Documentation | Qt Quarterly ...

  4. c++ Dynamic Memory (part 1)

    1. make_shared<T>(args): return a shared_ptr dynamically allocated object of type T. Use args ...

  5. TApplication.Initialize的前世今生

    ---------------------------------------------------------------------------------------------------- ...

  6. boost smart pointer

    1. boost::scoped_ptr is a smart pointer that is the sole owner of a dynamically allocated object and ...

  7. 不一样的dynamic解析json 万能方法

    写过javascript的人都知道js解析json 1:(JSON) 字符串转换为对象. var str = '{"name":"lsw","hobb ...

  8. Using pointer to access array instead of index

    See example below firstly. uint8_t parity = ; uint8_t index = ; //flag gMUXTask.responseData[index++ ...

  9. android 官方文档 JNI TIPS

    文章地址  http://developer.android.com/training/articles/perf-jni.html JNI Tips JNI is the Java Native I ...

随机推荐

  1. Git强制更新本地库和冲突解决

    1.You have not concluded your merge. (MERGE_HEAD exists) 本地有修改和提交,如何强制用远程的库更新.出现这种情况一般是git本地有commit, ...

  2. MySQL存储过程整理

    MySQL存储过程 2018-08-15  23:00:06 1.存储过程介绍 (1) 定义:存储过程是存储在数据库目录中的一段声明性SQL语句. 触发器,其他存储过程以及java,python,ph ...

  3. BZOJ2303 [Apio2011]方格染色 并查集

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ2303 题意概括 现在有一个N*M矩阵,矩阵上只能填数字0或1 现在矩阵里已经有一些格子被填写了数字 ...

  4. maven添加插件,与maven打包

    1.编译插件 添加编译器插件来告诉 Maven 使用哪个 JDK 版本是用来编译项目. 2.pom <plugin> <groupId>org.apache.maven.plu ...

  5. ListView优化中的细节问题

    1.android:layout_height属性: 必须将ListView的布局高度属性设置为非“wrap_content”(可以是“match_parent /  fill_parent  /   ...

  6. CentOS 7.1系统自动重启的Bug定位过程

    [问题] 有同事反应最近有多台MongoDB的服务器CentOS 7.1系统会自动重启,分析了下问题原因. [排查过程] 1. 检查系统日志/var/log/message,并没有记录异常信息,jou ...

  7. [软件研究]对AMH面板的研究

    0x00 前言 继续研究,这次来看一下AMH面板,图截自官网 就让我们来看看这个多个"首个"的面板做的怎么样吧. 0x01 安装 官方提供了两种安装方式,一是极速安装,二是编译安装 ...

  8. IdentityServer4目录

    一.介绍 二.快速入门 三.主题 四.端点 五.参考 IdentityServer4是ASP.NET Core 2的OpenID Connect和OAuth 2.0框架. 它可以在您的应用程序中启用以 ...

  9. JAVA编程:字符串转为数字求和

    程序从命令行接入数字,求和,如果接入的是字符串的解决办法 设计思想: 首先定义一个double类型的一维数组,然后在定义一个double型的变量sum,并赋值为0,用if循环判断从命令行是否有参数输入 ...

  10. SQL Server 限定删除一行

    with t as ( )* from testtest where aa='aa' order by bb ) delete from t