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 ...
随机推荐
- Linear Algebra(未完待续)
[矩阵消元] The result of multiplying a matrix by some vector is a combination of the columns of the matr ...
- 性能测试二十三:环境部署之Redis安装和配置
redis:非关系型数据库,内存数据库,no-sql典型, 数据存放在内存中,一断电或者关闭就没有了 mysql.oracle.sqlserver···是关系型数据库,数据存放在磁盘中 一个Red ...
- python 全栈开发,Day78(Django组件-forms组件)
一.Django组件-forms组件 forms组件 django中的Form组件有以下几个功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显 ...
- Linux学习笔记:使用prompt关闭ftp中mget和mput的确认提醒
当使用mget和mput上传或下载多个文件时,为了关闭确认提醒,可使用prompt命令. ftp prompt -- 切换提示 切换交谈式指令(使用mput/mget 时不用每个文件皆询问yes/n ...
- 《剑指offer》-递增数组中找到和为S的(最小)两个元素
题目描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的. 输出描述: 对应每个测试案例,输出两个数,小的先输出. 首先 ...
- ASS字幕制作和压制教程
首先放上一个成品预览,本教程只讲解基础的字幕制作,特效字幕请去TCAX论坛学习. http://www.tudou.com/programs/view/rcv33Uwf3hM/ ======= ...
- django中,如何把所有models模型文件放在同一个app目录下?
django的每个app目录下,都有自己的models.py文件. 原则上,每个app涉及的数据库,都会定义在这个文件里. 但是,有的数据库,涉及到多个app应用,不是很方便放在一个单独的app里. ...
- CentOS 用挂了dev/sda1:UNEXPECTED INCONSISTENCY;RUN fsck MANUALLY .
dev/sda1:UNEXPECTED INCONSISTENCY;RUN fsck MANUALLY .(i.e. ,without -a or -p options)fsck died with ...
- P1032 字串变换 字符串BFS
题目描述 已知有两个字串A,BA,B及一组字串变换的规则(至多66个规则): A_1A1 ->B_1B1 A_2A2 -> B_2B2 规则的含义为:在 AA中的子串 A_1A1 ...
- 【Java】 剑指offer(25) 合并两个排序的链表
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 输入两个递增排序的链表,合并这两个链表并使新链表中的结点仍然是按照 ...