博客
关于我
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/

你可能感兴趣的文章
MYSQL遇到Deadlock found when trying to get lock,解决方案
查看>>
mysql部署错误
查看>>
MySQL配置信息解读(my.cnf)
查看>>
Mysql配置文件my.ini详解
查看>>
MySQL配置文件深度解析:10个关键参数及优化技巧---强烈要求的福利来咯。
查看>>
Mysql配置表名忽略大小写(SpringBoot连接表时提示不存在,实际是存在的)
查看>>
mysql配置读写分离并在若依框架使用读写分离
查看>>
MySQL里为什么会建议不要使用SELECT *?
查看>>
MySQL里的那些日志们
查看>>
mysql重新安装?忘记root密码?重装Windows、Linux系统导致mysql没法用吗? 这里有你想要的答案
查看>>
mysql重置root密码
查看>>
MySQL锁
查看>>
MySQL锁与脏读、不可重复读、幻读详解
查看>>
MySQL锁机制
查看>>
mysql锁机制,主从复制
查看>>
Mysql锁机制,行锁表锁
查看>>
MySQL锁表问题排查
查看>>
Mysql锁(1):锁概述和全局锁的介绍
查看>>
Mysql锁(2):表级锁
查看>>
MySQL锁,锁的到底是什么?
查看>>