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 ...
随机推荐
- 欧拉函数,打表求欧拉函数poj3090
欧拉函数 φ(n) 定义:[1,N]中与N互质的数的个数 //互质与欧拉函数 /* 求欧拉函数 按欧拉函数计算公式,只要分解质因数即可 */ int phi(int n){ int ans=n; ;i ...
- splay好板子
找到一份比较好的板子,链接https://blog.csdn.net/crazy_ac/article/details/8034190 #include<cstdio> #include& ...
- 《剑指offer》-链表的第一个公共节点
题目描述 输入两个链表,找出它们的第一个公共结点. 这题目是指针相关的题目.初步要判断出来,有公共节点的两个指针,应当是链表后半部分相同.这样的话,当遇到第一个相同节点(不是node的val相同,而是 ...
- C#获取路径
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName -获取模块的完整路径. System.Environment.Cu ...
- #1 // BZOJ 4361 isn
Description 给出一个长度为n的序列A(A1,A2...AN).如果序列A不是非降的,你必须从中删去一个数, 这一操作,直到A非降为止.求有多少种不同的操作方案,答案模10^9+7. 题 ...
- CSS 2. 盒模型|浮动
1.盒模型 盒模型: 在网页中 基本上都会显示一些方方正正的盒子,这种盒子就被我们称为盒模型.重要的属性: width,height,padding,border, margin 盒子模型通过四个边界 ...
- 003 将spark源码导入到IDEA中
1.解压源代码 2.进入IDEA的首界面 3.使用open将解压的工程加载 4.将文件的形式改成maven项目 5.使用
- Scrapy爬虫笔记 - 爬取知乎
cookie是一种本地存储机制,cookie是存储在本地的 session其实就是将用户信息用户名.密码等)加密成一串字符串,返回给浏览器,以后浏览器每次请求都带着这个sessionId 状态码一般是 ...
- Ubuntu18.04更换国内源
Ubuntu18.04更换国内源 Ubuntu本身的源使用的是国内的源,下载速度比较慢,不像CentOS一样yum安装的时候对镜像站点进项选择, 所以选择了更换成国内的源. 以下内容整合自网络 备份/ ...
- VS Code 配置 C/C++ 环境(转)
写作原因 微软的 VSCode 一直以来为人诟病的一个问题就是对于 C/C++ 工程的编译以及调试支持度有限,配置起来比较复杂,但是 vscode-cpptools 团队经过一段时间的 bug 修 ...