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 newed 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[]的区别
基础教程推荐
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- c++ STL设置差异 2022-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
