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. Redis、RabbitMQ、Memcached

    知识目录: Memcached Redis RabbitMQ Memcached 回到顶部 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中 ...

  2. Visual Studio强大的帮助工具--Resharper安装与使用

    ReSharper是一个JetBrains公司出品的代码生成工具,其能帮助Microsoft Visual Studio成为一个更佳的IDE.使用ReSharper,你可以进行深度代码分析,智能代码协 ...

  3. Chakra调试笔记 TypedArray

    一.TypedArray类型 TypedArray是漏洞中常见到的结构,手册用法有四 1.new TypedArray(length); //byteLength=length * sizeof(Ty ...

  4. [转] h5上传视频或文件编写

    Html5 finally solves an age old problem of being able to upload files while also showing the upload ...

  5. day1作业--登录接口

    作业:编写登陆接口 输入用户名密码 认证成功后显示欢迎信息 输错三次后锁定     知识: 1.循环的使用: 2.continue,break在循环中中断的作用: 3.文件的写入,读取: 4.各基础知 ...

  6. python学习之基本数据类型

    python的基本数据类型有数字.字符串.列表.字典.元祖.布尔值 一.数字 1.1.字符转换为数字 实例: a=" b=int(a) print(b+) 运行结果: 可以用type查看数据 ...

  7. BZOJ2325 [ZJOI2011]道馆之战 树链剖分 线段树

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ2325 题意概括 给你一棵N个点的树,树上的每个节点有A,B两块区域,且每种区域有两种状态:可以走的 ...

  8. java中关于重载与重写的区别

    1.重载 允许多个同名但是形式参数个数或参数类型不同的方法存在同一个类中,在调用时根据形式参数列表来调用对应的方法. 2.判断 (1):方法名必须相同. (2):形式参数个数不同或者参数类型不同(满足 ...

  9. spring security 获取当前用户信息

    1.从页面上显示当前登陆的用户名 sec:authentication="name" <html xmlns:th="http://www.thymeleaf.or ...

  10. 64Bit & 32Bit HashCode

    以下为64位和32位的hash值计算方法 public class HashUtil { private static final long FNV_64_INIT = 0xcbf29ce484222 ...