C++ new delete 一维数组 二维数组 三维数组
h-----------------------------
#include "newandmalloc.h"
#include <iostream>
using namespace std; newAndMalloc::newAndMalloc()
{
cout<<"construct newAndMalloc..."<<endl;
} newAndMalloc::~newAndMalloc()
{
cout<<"destruct newAndMalloc..."<<endl;
deletePInt();
deletePPInt();
deletePPPInt();
} void newAndMalloc::newPInt()
{
cout<<"new m_pInt..."<<endl; m_pInt = new int[m_size]; //开辟空间 for (int i = ; i < m_size; i++) //赋值
{
m_pInt[i] = m_size - i;
}
} void newAndMalloc::newPPInt()
{
cout<<"new m_ppInt..."<<endl; m_ppInt = new int*[m_size]; //开辟一维的空间
for (int i = ; i < m_size; i++)
{
m_ppInt[i] = new int[m_size]; //开辟二维的控件
} for (int i = ; i < m_size; i++) //赋值
{
for (int j = ; j < m_size; j++)
{
m_ppInt[i][j] = i + j;
}
} } void newAndMalloc::newPPPInt()
{
cout<<"new m_pppInt..."<<endl; m_pppInt = new int**[m_size]; //开辟一维的空间
for(int i = ; i < m_size; i++)
{
m_pppInt[i] = new int*[m_size]; //开辟二维的空间
for (int j = ; j < m_size; j++)
{
m_pppInt[i][j] = new int[m_size]; //开辟三维的空间
}
} for (int i = ; i < m_size; i++) //赋值
{
for (int j = ; j < m_size; j++)
{
for (int h = ; h < m_size; h++)
{
m_pppInt[i][j][h] = i + j + h;
}
}
}
} void newAndMalloc::printPInt()
{
cout<<"print m_pInt..."<<endl; for (int i = ; i < m_size; i++)
{
cout<<m_pInt[i]<<" ";
}
cout<<endl;
} void newAndMalloc::printPPInt()
{
cout<<"print m_ppInt..."<<endl; for (int i = ; i < m_size; i++)
{
for (int j = ; j < m_size; j++)
{
cout<<m_ppInt[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
} void newAndMalloc::printPPPInt()
{
cout<<"print m_pppInt..."<<endl; for (int i = ; i < m_size; i++)
{
for (int j = ; j < m_size; j++)
{
for (int h = ; h < m_size; h++)
{
cout<<m_pppInt[i][j][h]<<" ";
}
cout<<endl;
}
cout<<endl;
}
cout<<endl;
} void newAndMalloc::deletePInt()
{
cout<<"delete m_pInt..."<<endl;
delete[] m_pInt;
} void newAndMalloc::deletePPInt()
{
cout<<"delete m_ppInt..."<<endl;
for (int i = ; i < m_size; i++)
{
delete[] m_ppInt[i];
}
delete[] m_ppInt;
} void newAndMalloc::deletePPPInt()
{
cout<<"delete m_pppInt..."<<endl;
for (int i = ; i < m_size; i++)
{
for (int j = ; j < m_size; j++)
{
delete[] m_pppInt[i][j];
}
delete[] m_pppInt[i];
}
delete[] m_pppInt;
} testClass::testClass()
{
cout<<"construct testClass..."<<endl;
} testClass::~testClass()
{
cout<<"destruct testClass..."<<endl;
} cpp--------------------------------
#include "newandmalloc.h"
#include <iostream>
using namespace std; newAndMalloc::newAndMalloc()
{
cout<<"construct newAndMalloc..."<<endl;
} newAndMalloc::~newAndMalloc()
{
cout<<"destruct newAndMalloc..."<<endl;
deletePInt();
deletePPInt();
deletePPPInt();
} void newAndMalloc::newPInt()
{
cout<<"new m_pInt..."<<endl; m_pInt = new int[m_size]; //开辟空间 for (int i = ; i < m_size; i++) //赋值
{
m_pInt[i] = m_size - i;
}
} void newAndMalloc::newPPInt()
{
cout<<"new m_ppInt..."<<endl; m_ppInt = new int*[m_size]; //开辟一维的空间
for (int i = ; i < m_size; i++)
{
m_ppInt[i] = new int[m_size]; //开辟二维的控件
} for (int i = ; i < m_size; i++) //赋值
{
for (int j = ; j < m_size; j++)
{
m_ppInt[i][j] = i + j;
}
} } void newAndMalloc::newPPPInt()
{
cout<<"new m_pppInt..."<<endl; m_pppInt = new int**[m_size]; //开辟一维的空间
for(int i = ; i < m_size; i++)
{
m_pppInt[i] = new int*[m_size]; //开辟二维的空间
for (int j = ; j < m_size; j++)
{
m_pppInt[i][j] = new int[m_size]; //开辟三维的空间
}
} for (int i = ; i < m_size; i++) //赋值
{
for (int j = ; j < m_size; j++)
{
for (int h = ; h < m_size; h++)
{
m_pppInt[i][j][h] = i + j + h;
}
}
}
} void newAndMalloc::printPInt()
{
cout<<"print m_pInt..."<<endl; for (int i = ; i < m_size; i++)
{
cout<<m_pInt[i]<<" ";
}
cout<<endl;
} void newAndMalloc::printPPInt()
{
cout<<"print m_ppInt..."<<endl; for (int i = ; i < m_size; i++)
{
for (int j = ; j < m_size; j++)
{
cout<<m_ppInt[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
} void newAndMalloc::printPPPInt()
{
cout<<"print m_pppInt..."<<endl; for (int i = ; i < m_size; i++)
{
for (int j = ; j < m_size; j++)
{
for (int h = ; h < m_size; h++)
{
cout<<m_pppInt[i][j][h]<<" ";
}
cout<<endl;
}
cout<<endl;
}
cout<<endl;
} void newAndMalloc::deletePInt()
{
cout<<"delete m_pInt..."<<endl;
delete[] m_pInt;
} void newAndMalloc::deletePPInt()
{
cout<<"delete m_ppInt..."<<endl;
for (int i = ; i < m_size; i++)
{
delete[] m_ppInt[i];
}
delete[] m_ppInt;
} void newAndMalloc::deletePPPInt()
{
cout<<"delete m_pppInt..."<<endl;
for (int i = ; i < m_size; i++)
{
for (int j = ; j < m_size; j++)
{
delete[] m_pppInt[i][j];
}
delete[] m_pppInt[i];
}
delete[] m_pppInt;
} testClass::testClass()
{
cout<<"construct testClass..."<<endl;
} testClass::~testClass()
{
cout<<"destruct testClass..."<<endl;
} main-------------------------------
#include <iostream>
#include <stdio.h>
#include "newandmalloc.h" int main(int argc, char *argv[])
{
newAndMalloc *nam = nullptr;
nam = new newAndMalloc();
nam->newPInt();
nam->newPPInt();
nam->newPPPInt(); nam->printPInt();
nam->printPPInt();
nam->printPPPInt(); delete nam;
return ;
} 打印输出------------------------
construct newAndMalloc...
new m_pInt...
new m_ppInt...
new m_pppInt...
print m_pInt... print m_ppInt... print m_pppInt... destruct newAndMalloc...
delete m_pInt...
delete m_ppInt...
delete m_pppInt...
C++ new delete 一维数组 二维数组 三维数组的更多相关文章
- C语言malloc函数为一维,二维,三维数组分配空间
c语言允许建立内存动态分配区域,以存放一些临时用的数据,这些数据不必在程序的声明部分定义,也不必等到函数结束时才释放,而是需要时随时开辟,不需要时随时释放,这些数据存储在堆区.可以根据需要,向系统申请 ...
- python如何删除二维或者三维数组/列表中某维的空元素
如题,个人在使用python进行数据预处理过程中出现的问题,抽象成删除三维列表中某维为空的问题. 一.首先来看一下三维数组/列表的结构 仔细看下图就会很清楚了: 轴0即是去除第一个外括号后第一层(我把 ...
- BZOJ3132 上帝造题的七分钟 【二维树状数组】
题目 "第一分钟,X说,要有矩阵,于是便有了一个里面写满了0的n×m矩阵. 第二分钟,L说,要能修改,于是便有了将左上角为(a,b),右下角为(c,d)的一个矩形区域内的全部数字加上一个值的 ...
- Java基本语法-----java数组(一维数组二维数组)
嘿嘿!你们懂的,又是图片,委屈大家了. java数组(一维数组二维数组) [正在看本人博客的这位童鞋,我看你气度不凡,谈吐间隐隐有王者之气,日后必有一番作为!下面有个"顶"字,你就 ...
- Java一维与二维数组的拷贝与排序
Java一维与二维数组的拷贝与排序 目录 Java一维与二维数组的拷贝与排序 Arrays.sort() 一维数组升序排序 二维数组按行升序排序 二维数组按列升序排序 Java中的数组 Java中数组 ...
- POJMatrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 22058 Accepted: 8219 Descripti ...
- poj 1195:Mobile phones(二维树状数组,矩阵求和)
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14489 Accepted: 6735 De ...
- [poj2155]Matrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 25004 Accepted: 9261 Descripti ...
- [POJ2155]Matrix(二维树状数组)
题目:http://poj.org/problem?id=2155 中文题意: 给你一个初始全部为0的n*n矩阵,有如下操作 1.C x1 y1 x2 y2 把矩形(x1,y1,x2,y2)上的数全部 ...
- BZOJ 1452 Count(二维树状数组)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1452 题意:给出一个数字矩阵(矩阵中任何时候的数字均为[1,100]),两种操作:(1) ...
随机推荐
- 脱离 WebView 的通信 JavaScriptCore
JavascriptCore JavascriptCore 一直作为 WebKit 中内置的 JS 引擎使用,在 iOS7 之后,Apple 对原有的 C/C++ 代码进行了 OC 封装,成为系统级的 ...
- 【转】Visual Studio Code(VS code)你们都在用吗?或许你们需要看一下这篇博文
写在前面 在前端开发中,有一个非常好用的工具,Visual Studio Code,简称VS code. 都不用我安利VS code,大家就会乖乖的去用,无数个大言不惭的攻城狮,都被VS code比德 ...
- 记录RFID操作错误
如果代码操作不了RFID设备,查看下通信协议的设置
- datatable转layui表格【偏原理】
如题这个类负责把datatable转换为layui表格可以显示的内容.适合配合表格url字段的webapi服务端,为其返回响应字符串.代码如下:using System;using System.We ...
- Golang程序调试工具介绍(gdb vs dlv)
原文:http://lday.me/2017/02/27/0005_gdb-vs-dlv/ 通过log库输出日志,我们可以对程序进行异常分析和问题追踪.但有时候,我也希望能有更直接的程序跟踪及定位工具 ...
- PostgreSQL查询当前时间的时间戳
一.问题 使用PostgreSQL获取当前系统时间戳.众所周知,在MySQL中是这样的: select UNIX_TIMESTAMP(NOW()) 二.解决方案 (1)精确到秒 " (2)精 ...
- Python关于多继承
大部分面向对象的编程语言(除了C++)都只支持单继承,而不支持多继承,为什么呢?因为多继承不仅增加编程复杂度,而且容易导致莫名其妙的错误. Python虽然语法上支持多继承,但是却不推荐使用多继承,而 ...
- 默认展开ztree树形菜单
var setting = { view: { selectedMulti: false //按住ctrl是否可以多选 }, check: { enable: true , chkStyle: 'ch ...
- k8s控制器资源(五)
Pod pod在之前说过,pod是kubernetes集群中是最小的调度单元,pod中可以运行多个容器,而node又可以包含多个pod,关系如下图: 在对pod的用法进行说明之前,有必要先对docke ...
- 使用Deployment控制器创建Pods并使Service发布到外网可访问
由于NFS支持节点共同读取及写入,所以可使用Deployment控制器创建多个Pod,并且每一个Pod都共享同一个目录 k8s-master kubnet@hadoop2 volumes]$ vim ...