博客
关于我
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安装之没有配置向导
查看>>
mysql安装出现 conflicts with mysql*的解决办法
查看>>
mysql安装卡在最后一步解决方案(附带万能安装方案)
查看>>
mysql安装和启动命令小结
查看>>
Mysql安装教程(命令行)
查看>>
mysql安装版安装
查看>>
MySQL安装配置教程(非常详细),从零基础入门到精通,看完这一篇就够了
查看>>
mysql安装配置简介
查看>>
MySQL定义和变量赋值
查看>>
mysql定时任务事件清理单表数据
查看>>
MySQL定时器Events
查看>>
Mysql定时备份脚本
查看>>
mysql实战01|基础架构:一条SQL查询语句是如何执行的?
查看>>
Mysql实战之数据备份
查看>>
MySQL实战教程:从小白到大神的进阶之路!
查看>>
mysql实现成绩排名
查看>>
Mysql客户端中文乱码问题解决
查看>>
mysql客户端工具使用
查看>>
MySQL密码忘记,怎么办?
查看>>
mysql对同一张表进行查询和赋值更新
查看>>