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 ...
随机推荐
- laravel migrate 指定执行部分 migration
由于我不习惯于使用 laravel migration 来管理数据表变更,所以很多项目都是无法直接执行 php artisan migrate 否则会报错 SQLSTATE[42S01]: Base ...
- poj3349 散列表(hash)
就是散列表的应用,把每片哈希值相同的雪花排到一条链上去即可,每片雪花x的哈希值 hash(x)=sum(x的六角)+mul(x的六角),会爆int #include<iostream> # ...
- Linux下配置自动更新时间
1,修正本地时区及ntp服务 [root@VM_0_13_centos ~]# yum -y install ntp [root@VM_0_13_centos ~]# rm -rf /etc/loca ...
- unittest中更高效的执行测试用例一个类只需要打开一次浏览器
示例代码 baidu.py # _*_ coding:utf-8 _*_ import csv,unittest #导入csv模块 from time import sleep from seleni ...
- Maven多模块项目
1.项目结构-父项目 其中parent是父项目,这个父项目的父项目是springboot,我搭建这个多模块的项目的目的主要是为了研究学习springbatch 父项目的pom文件内容: <pro ...
- 【AtCoder】CODE FESTIVAL 2017 qual C
A - Can you get AC? No #include <bits/stdc++.h> #define fi first #define se second #define pii ...
- BZOJ1826 [JSOI2010]缓存交换 堆 贪心
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1826 题意概括 Cache中有m个储存单元,接下来有n个访问地址,每个地址用一个数字表示.访问每一 ...
- 【Java】 剑指offer(19) 正则表达式匹配
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 请实现一个函数用来匹配包含'.'和'*'的正则表达式.模式中的字符 ...
- centos7 安装步骤
这里选择64位 32位没有找到网卡... 注:这里是网络类型分配,网络类型分配分为三种,Bridge,NAT和Host-Only,大概区别是 1 BRIDGE 桥接:相当于主机和虚拟机连接到同一个h ...
- CSS之文本溢出隐藏,不定宽高元素垂直水平居中、禁止页面文本复制
1.如何让不固定元素宽高的元素垂直水平居中 .center { position: absolute; top: 50%; left: 50%; background-color: #000; wid ...