The correct way to initialize a dynamic pointer to a multidimensional array
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];
- 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!
The correct way to initialize a dynamic pointer to a multidimensional array的更多相关文章
- C lang:Pointer and multidimensional array
		Xx_Introduction Double indrection:Address of Address;Pointer of Pointer Ax_Code #include<stdio.h& ... 
- Linux Programe/Dynamic Shared Library Entry/Exit Point && Glibc Entry Point/Function
		目录 . 引言 . C/C++运行库 . 静态Glibc && 可执行文件 入口/终止函数 . 动态Glibc && 可执行文件 入口/终止函数 . 静态Glibc & ... 
- Dynamic Signals and Slots
		Ref https://doc.qt.io/archives/qq/qq16-dynamicqobject.html Trolltech | Documentation | Qt Quarterly ... 
- c++ Dynamic Memory (part 1)
		1. make_shared<T>(args): return a shared_ptr dynamically allocated object of type T. Use args ... 
- TApplication.Initialize的前世今生
		---------------------------------------------------------------------------------------------------- ... 
- boost smart pointer
		1. boost::scoped_ptr is a smart pointer that is the sole owner of a dynamically allocated object and ... 
- 不一样的dynamic解析json 万能方法
		写过javascript的人都知道js解析json 1:(JSON) 字符串转换为对象. var str = '{"name":"lsw","hobb ... 
- Using pointer to access array instead of index
		See example below firstly. uint8_t parity = ; uint8_t index = ; //flag gMUXTask.responseData[index++ ... 
- android 官方文档  JNI TIPS
		文章地址 http://developer.android.com/training/articles/perf-jni.html JNI Tips JNI is the Java Native I ... 
随机推荐
- confusion_matrix(混淆矩阵)
			作者:十岁的小男孩 凡心所向,素履可往 目录 监督学习—混淆矩阵 是什么?有什么用?怎么用? 非监督学习—匹配矩阵 混淆矩阵 矩阵每一列代表预测值,每一行代表的是实际的类别.这个名字来源于它可以非常容 ... 
- bzoj 1112 poi 2008 砖块
			这滞胀题调了两天了... 好愚蠢的错误啊... 其实这道题思维比较简单,就是利用treap进行维护(有人说线段树好写,表示treap真心很模板) 就是枚举所有长度为k的区间,查出中位数,计算代价即可. ... 
- for..in  遍历js对象
			for..in 遍历js对象 利用for in 给对象遍历: 
- ***PHP基于H5的微信支付开发详解(CI框架)
			这次总结一下用户在微信内打开网页时,可以调用微信支付完成下单功能的模块开发,也就是在微信内的H5页面通过jsApi接口实现支付功能.当然了,微信官网上的微信支付开发文档也讲解的很详细,并且有实现代码可 ... 
- python全栈开发day36-IO多路复用
			一.复习 1.进程.线程.协程 进程:是计算机中最小的资源分配单位,数据隔离,可以利用多核,数据不安全 线程:是计算机中最小的CPU调度单位,数据共享,GIL,数据不安全 协程:是线程的一部分,是由用 ... 
- Pig和Hive的对比
			Pig Pig是一种编程语言,它简化了Hadoop常见的工作任务.Pig可加载数据.表达转换数据以及存储最终结果.Pig内置的操作使得半结构化数据变得有意义(如日志文件).同时Pig可扩展使用Java ... 
- 【Java】 剑指offer(16) 打印1到最大的n位数
			本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 输入数字n,按顺序打印出从1最大的n位十进制数.比如输入3,则打印 ... 
- Python2 - 基础2 - 数据类型和模块
			一.数据类型 标准数据类型(5): Numbers(数字) String(字符串) List(列表) Tuple(元组) Dictionary(字典) 其中数字类型有4种: int(有符号整型) 在3 ... 
- android和java以太坊开发区块链应用使用web3j类库
			如何使用web3j为Java应用或Android App增加以太坊区块链支持,教程内容即涉及以太坊中的核心概念,例如账户管理包括账户的创建.钱包创建.交易转账,交易与状态.智能合约开发与交互.过滤器和 ... 
- fmod()函数  (对浮点数取模)
			头文件:#include <math.h> fmod() 用来对浮点数进行取模(求余),其原型为: double fmod (double x); 设返回值为 ret,那么 x = ... 
