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) ...
随机推荐
- 前端学习:学习笔记(HTML部分)
前端学习:学习笔记(HTML部分) HTML学习总结(图解) HTML简介 1.HTML是什么? 超文本标记语言 超文本:文字/图片/音频/视频.... 标记/标签 2.HTML的用途? 是用来编写静 ...
- RAID(独立磁盘冗余阵列)简介
RAID(独立磁盘冗余阵列) 在大数据技术出现之前,人们就需要面对这些关于存储的问题,对应的解决方案就是RAID技术. RAID(独立磁盘冗余阵列)技术主要是为了改善磁盘的存储容量,读写速度,增强磁盘 ...
- Prometheus监控学习笔记之prometheus 版本1.7 常用启动参数
日志类: -log.level 可选值 [debug, info, warn, error, fatal] 例:-log.level "info" -log.format 可选 ...
- ActionMq + mqttws3.1 实现持久化订阅
activemq版本:5.15.3 Eclipse Paho MQTT JavaScript library mqttws3.1:在amq安装目录下webapp-demo目录下可以找到 实现步骤请阅读 ...
- ASP.Net Core中设置JSON中DateTime类型的格式化(解决时间返回T格式)
最近项目有个新同事,每个API接口里返回的时间格式中都带T如:[2019-06-06T10:59:51.1860128+08:00],其实这个主要是ASP.Net Core自带时间格式列化时间格式设置 ...
- Asp.net MVC企业级开发(02)---Log4net
Log4Net 是用来记录日志的,可以将程序运行过程中的信息输出到一些地方(文件.数据库.EventLog等).日志就是程序的“黑匣子”,可以通过日志查看系统的运行过程,从而发现系统的问题. 日志的作 ...
- Loadrunner 11.00 初始化失败; 通信错误。 Error (-81024): LR_VUG: The 'WS_SOAP' type is not supported on 'WIN32' platforms .
搜索LR安装目录bin文件夹下有个“wlrun.exe”的文件,邮件点击“属性”->"兼容性"->兼容模式中选择“windows 7”,确认后重新打开即可,win10下 ...
- Object.freeze
Object.freeze() 方法可以冻结一个对象.一个被冻结的对象再也不能被修改:冻结了一个对象则不能向这个对象添加新的属性,不能删除已有属性,不能修改该对象已有属性的可枚举性.可配置性.可写性, ...
- Docker 的操作命令记录
docker ps:列出正在运行的 container docker ps -a:列出所有的 container docker rm [containerid]:移除 container(可并列多个, ...
- qt 操作串口 QSerialPort
准备工作 *.pro中加入 QT += serialport 初始化 void MainWindow::initPort() { //读取串口信息 foreach (const QSerialPort ...