为什么在C ++中有一个delete []? [英] Why is there a delete[] in C++?

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

问题描述

为什么会有 delete [] ?从我的理解它的行为不同的数组。但是,为什么它真的存在?有只有C和没有free_array的免费。在语法中, delete var delete [] var 之间的唯一区别是 []

Why is there a delete[]? From my understanding its to behave differently for arrays. However, why does it really exist? There's only free in C and no free_array. Also in syntax the only difference between delete var and delete []var is the [] which has no params (I'm not telling the length of the array).

所以为什么 delete [ ] 真的存在?我知道有人会说你可以重载 delete delete [] (至少我认为这是可能的)说我们不会超载它。为什么它存在?

So why does delete[] really exist? I know someone will say you can overload delete and delete[] (at least i think that is possible) but lets say we are not overloading it. Why does it exist?

推荐答案

通常,对于非POD类, delete [] expression必须在不能在编译时确定的可变数量的类实例上调用析构函数。编译器通常必须实现一些运行时magic,可用于确定要销毁的对象的正确数量。

Typically, for non-POD classes, a delete[] expression must call destructors on a variable number of class instances that cannot be determined at compile time. The compiler typically has to implement some run time "magic" that can be used to determine the correct number of objects to destroy.

A delete 表达式不必担心这个,它只是必须销毁提供的指针指向的一个对象。

A delete expression doesn't have to worry about this, it simply has to destroy the one object that the supplied pointer is pointing to. Because of this, it can have a more efficient implementation.

通过拆分 delete delete [] ,可以实现,而不需要正确实现 delete []

这篇关于为什么在C ++中有一个delete []?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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