C ++中delete和delete []之间的区别 [英] The difference between delete and delete[] in C++

查看:115
本文介绍了C ++中delete和delete []之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
在C ++中删除vs删除[]运算符

Possible Duplicate:
delete vs delete[] operators in C++

我编写了一个包含两个指针的类,一个是char* color_,另一个在vertexesset* vertex_中,其中vertexesset是我创建的类.在我一开始就写的tract贬不一中

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?

推荐答案

delete []设置数组类型时为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 []之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆