动态内存分配
2013-10-04 16:41
260 查看
#include <iostream> using namespace std; void mian() { char* pc; int* pi; //new 类型标示符 pc =new char; *pc = 'a'; cout<<*pc<<endl; //new 类型标示符(初始值) pi = new int(8); cout<<*pi<<endl; //new 类型标示符[内存单元个数] char* pStr= new char[20]; char str[20]; strcpy(pStr,"It is a string."); strcpy(str,"It is a string too."); cout<<pStr<<endl; cout<<str<<endl; //判断是否是有效的地址 //如果成功,就返回有效内存地址 //否则会返回0, //检查是否指针是否等于0就行 if (pc) { delete pc; } if (pi) { delete pi; } if (pStr) { //释放数组空间,要带[],否则只是释放数组头元素 delete []pStr; } }
相关文章推荐
- 预编译
- 指针1
- 设置屏幕方向
- Cocos2d-x中的词典类CCDictionary深入分析
- cocos2d-x获取当前动画是第几帧
- pvr与png的内存占用
- 自定义控件在XML布局文件的运用
- 解决使用AnimationDrawable无法播放动画的问题(转)
- Drawable和Bitmap转换
- 浅谈Android五大布局(一)——LinearLayout、FrameLayout和AbsoulteLayout
- Android的Dialog开发和运用
- 安卓读取SD卡路径
- JS教程:window.location使用方法的区别介绍
- 利用LayerDrawable一次加载多张图片和改变SeekBar的外观
- 利用LayerDrawable一次加载多张图片和改变SeekBar的外观
- Git 学习
- 大规模超文本网络搜索引擎解析 [ The Anatomy of a Large-Scale Hypertextual Web Search Engine ]
- EOF \n \0 NULL 之间的区别
- Light, more light UVA10110
- 数据库连接字符串大全