qt开发环境 - c++动态内存
2017-08-15 22:32
393 查看
#include <QCoreApplication> #include <iostream> using namespace std; struct Student{ string name; int age; }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); int* p1 = new int; *p1 = 666; ++*p1; cout << *p1 << endl; delete p1; p1 = NULL; p1 = new int();//默认是0 cout << *p1 << endl; delete p1; p1 = NULL; p1 = new int(666);//默认是666 cout << *p1 << endl; delete p1; p1 = NULL; delete p1;//不会出错,delete会自动检查 是不是空 // *p1 = 233; p1 = new int[4]{1,2,3,4}; cout << p1[0] << p1[1] << p1[2] << p1[3]<< endl; delete[] p1; // p1 = new int[0xFFFFFFFF]; try{ p1 = new int[/*0xFFFFFFFF*/3]; //成功,访问内存(可以在这里) //delete[] p1; //p1 = NULL; } catch(exception& ex){ cout << ex.what() << endl; cout << "动态内存分配失败" << endl; return -1; } //成功,访问内存(也可以在这里) //delete[] p1; //p1 = NULL; int (*p2)[4] = new int[3][4];//4是内层结构 p2[0][0] = 2; cout << p2[0][0] << endl; delete[] p2; int (*p3)[4][5] = new int[3][4][5]; p3[0][0][1] = 777; cout << p3[0][0][1] << endl; delete[] p3; string* p4 = new string; cout << '[' << *p4 << ']' << endl; delete p4; p4 = new string("ayumi hamasaki"); cout << '[' << *p4 << ']' << endl; delete p4; p4 = new string[3]{"I ","love ", "ayumi"}; cout << p4[0] << p4[1] << p4[2] << endl; delete p4; Student* p5 = new Student; p5->name = "ayu"; p5->age = 33; cout << p5->name << p5->age << endl; delete p5; return a.exec(); }
相关文章推荐
- ubuntu下两种C++集成开发环境IDE,Qt简直时神器
- SuperMap iObjects C++结合Qt在QtCreator上的开发环境搭建
- qt-eclipse-c++环境配置(图形界面开发)
- CC++初学者编程教程(10) 搭建Android java C/C++ NDK QTforAndroid 开发环境
- TreeFrog Framework(C++ web framework, based on Qt)开发环境搭建
- C/C++ 开发环境配置(GTK/GNOME/Qt/KDE)
- qt开发环境 - c++之无名名字空间,名字空间嵌套邻近原则(内藏外),名字空间别名
- qt开发环境 - c++之结构,联合,枚举
- CC++初学者编程教程(10) 搭建Android java C/C++ NDK QTforAndroid 开发环境
- 构建 C++ 开发环境包括 gtk、qt 等开发库
- C/C++开发环境(GTK/GNOME/Qt/KDE)
- qt-eclipse-c++环境配置(图形界面开发)
- SuperMap iObjects C++结合Qt在vs2012上的开发环境搭建
- ubuntu14.04 qt4 C++开发环境搭建
- qt开发环境 - c++函数换名,extern 'C',缺省参数,内联
- Qt/C++ 开发Android平台《林中伊人》消球小游戏全记录2——Qt on Android开发环境的搭建
- C/C++开发环境(GTK/GNOME/Qt/KDE)
- qt开发环境 - c++之输入,输出,名字空间定义,名字空间指令,名字空间声明
- qt开发环境 - c++字符串操作
- 安装Qt+MinGW+Eclipse for c_c++开发环境配置总结