The difference between delete and delete[] in C++(C++中delete和delete[]的区别)
问题描述
可能的重复:
C++ 中的删除与删除[] 运算符
我写了一个包含两个指针的类,一个是 char* color_
一个是 vertexesset* vertex_
其中 vertexesset
是一个我创建的类.在我开始时写的析构函数中
I've written a class that contains two pointers, one is char* color_
and one in vertexesset* vertex_
where vertexesset
is a class I created. In the destractor I've written at start
delete [] color_;
delete [] vertex_;
当涉及到析构函数时,它给了我一个分段错误.
When It came to the destructor it gave me a segmentation fault.
然后我将析构函数改为:
Then I changed the destructor to:
delete [] color_;
delete vertex_;
现在它工作正常.两者有什么区别?
And now it works fine. What is the difference between the two?
推荐答案
当你new
一个数组类型时,你delete []
,然后delete
代码> 当你没有.示例:
You delete []
when you new
ed an array type, and delete
when you didn't. Examples:
typedef int int_array[10];
int* a = new int;
int* b = new int[10];
int* c = new int_array;
delete a;
delete[] b;
delete[] c; // this is a must! even if the new-line didn't use [].
这篇关于C++中delete和delete[]的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++中delete和delete[]的区别
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 近似搜索的工作原理 2021-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- STL 中有 dereference_iterator 吗? 2022-01-01
- 静态初始化顺序失败 2022-01-01
- 从python回调到c++的选项 2022-11-16
- Stroustrup 的 Simple_window.h 2022-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- C++ 协变模板 2021-01-01