CPP-基础:C++的new int()与new int[]
2013-10-09 16:26
483 查看
编写一个List类:
在调试时,运行报错:
Debug Error!
Frogram: C:\Test\Debug\Test.exe
DAMAGE: after Normal block(#xxxx) at 0x00430040 (Press Retry to debug the application)
出错的原因:
lpInt = new int(length);
应该修改为:
lpInt = new int[length];
说明:
int* lpInt = new int(10)是分配一个int,也就是*lpInt = 10。
int* lpInt = new int[10]是分配10个int数组,lpInt是数组的首地址。
class List { int length; //列表长度 int* lpInt; //列表指针 List(int size); ~List(); }
List::List(int size) { length = size; lpInt = new int(length); //关键点 int n; for(n=0;n<length;n++) { lpInt = n; } } List::~List() { delete []lpInt; //出错,访问越界 }
在调试时,运行报错:
Debug Error!
Frogram: C:\Test\Debug\Test.exe
DAMAGE: after Normal block(#xxxx) at 0x00430040 (Press Retry to debug the application)
出错的原因:
lpInt = new int(length);
应该修改为:
lpInt = new int[length];
说明:
int* lpInt = new int(10)是分配一个int,也就是*lpInt = 10。
int* lpInt = new int[10]是分配10个int数组,lpInt是数组的首地址。
相关文章推荐
- CPP-基础:c++读取ini文件
- cpp基础: c++ 定义与声明的区别
- CPP-基础:new int[]跟int()的区别
- 更高效的C++_More Effective CPP手记一之基础议题
- CPP-基础:C++中为什么需要一个头文件,一个cpp文件
- CPP-基础:新标准 C++iostream
- CPP-基础:C++拷贝构造函数详解
- [C++]cpp小笔记3 --- C++基础类型的大小和转换
- CPP-基础:C/C++数组名与指针的区别
- c++基础2:使用VS2010 创建最简单的MFC应用程序窗体
- c/c++unix/linux基础学习笔记-常用命令和vi的使用
- C++——算法基础之最小生成树(Prim 和 Kruskal)
- C++——算法基础之动态查找表1——二叉排序树
- C++(10):类基础
- 嵌入式开发之C++基础学习笔记1--综合比较HelloWorld示例
- c++基础知识 1-5
- C/C++基础及高频率面试知识总结
- C++基础(12)
- CppSQLite - C++ Wrapper for SQLite
- C++基础之多态