博客
关于我
C++ 动态内存分配基础
阅读量:242 次
发布时间:2019-03-01

本文共 1166 字,大约阅读时间需要 3 分钟。

new的使用

#include
#include
using namespace std;int main(){ int *p = new int(200); cout << *p << endl; // 单个整形变量的动态申请 string *ps = new string("purple paplace"); cout << *ps << endl; // string形字符串的申请 struct Stu { int age; string name; }; Stu* pStu = new Stu{ 10,"bob" }; // 结构体的申请 cout << pStu->age << endl; cout << pStu->name << endl; system("pause");}

动态申请空间

#include
#include
using namespace std;int main(){ /*char *p = new char[40]; strcpy(p, "china"); // 字符串的动态申请 cout << p << endl;*/ int *pi = new int[5]; // 一维数组的动态申请 memset(pi, 0, sizeof(int[5])); for (int i = 0; i < 5; i++) { cout << pi[i] << endl; } delete []pi; // 一维数组的释放 /*char **ppc = new char*[5]{NULL}; // 字符串指针的动态申请 ppc[0] = new char[10]; strcpy(ppc[0], "china");*/ int(*pa)[4] = new int[3][4]; // 二维数组的动态申请 memset(pa, 0, sizeof(int[3][4])); for (int i = 0; i < sizeof(int[3][4]) / sizeof(int[4]); i++) { for (int j = 0; j < 4; j++) { cout << pa[i][j] << " "; } cout << endl; } int(*px)[3][4][5] = new int[2][3][4][5]; // 多维数组的动态申请 system("pause");}

 

转载地址:http://xmhv.baihongyu.com/

你可能感兴趣的文章
net包之IPConn
查看>>
net发布的dll方法和类显示注释信息(字段说明信息)[图解]
查看>>
Net和T-sql中的日期函数操作
查看>>
Net处理html页面元素工具类(HtmlAgilityPack.dll)的使用
查看>>
Net操作Excel(终极方法NPOI)
查看>>
Net操作配置文件(Web.config|App.config)通用类
查看>>
net网络查看其参数state_dict,data,named_parameters
查看>>
Net连接mysql的公共Helper类MySqlHelper.cs带MySql.Data.dll下载
查看>>
NeurIPS(神经信息处理系统大会)-ChatGPT4o作答
查看>>
neuroph轻量级神经网络框架
查看>>
Neutron系列 : Neutron OVS OpenFlow 流表 和 L2 Population(7)
查看>>
new Blob()实现不同类型的文件下载功能
查看>>
New Concept English three (35)
查看>>
NEW DATE()之参数传递
查看>>
New Journey--工作五年所思所感小记
查看>>
new Queue(REGISTER_DELAY_QUEUE, true, false, false, params)
查看>>
New Relic——手机应用app开发达人的福利立即就到啦!
查看>>
new work
查看>>
new 一个button 然后dispose,最后这个button是null吗???
查看>>
NewspaceGPT的故事续写能力太强了
查看>>