博客
关于我
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截取sql语句
查看>>
mysql截取身份证号前几位_EXCEL中怎样截取身份证号前六位数字
查看>>
mysql手工注入
查看>>
MySQL执行SQL文件出现【Unknown collation ‘utf8mb4_0900_ai_ci‘】的解决方案
查看>>
Mysql执行update by id的过程
查看>>
mysql执行计划
查看>>
MySQL执行计划 EXPLAIN参数
查看>>
MySQL执行计划【explain】,看这一篇就够啦!
查看>>
Mysql执行计划字段解释
查看>>
mysql执行计划怎么看
查看>>
MySQL执行计划解读
查看>>
mysql执行顺序与索引算法
查看>>
mysql批量update优化_Mysql中,21个写SQL的好习惯,你值得拥有呀
查看>>
mysql批量update操作时出现锁表
查看>>
MYSQL批量UPDATE的两种方式
查看>>
mysql批量修改字段名(列名)
查看>>
MySQL批量插入数据遇到错误1213的解决方法
查看>>
mysql技能梳理
查看>>
MySQL报Got an error reading communication packets错
查看>>
Mysql报错Can‘t create/write to file ‘/tmp/#sql_3a8_0.MYD‘ (Errcode: 28 - No space left on device)
查看>>