关于删除[]的问题 [英] question regarding delete[]

查看:64
本文介绍了关于删除[]的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨专家,


i对c ++中的delete []运算符几乎没有任何疑问。

为什么c ++必须向运算符删除memeory viz delete和

删除[]。

为什么删除后使用了删除[]。

删除运算符[]如何知道数量

析构函数的对象

被调用。

谢谢

ravinder

推荐答案

ra ***** *******@gmail.com 写道:
为什么c ++必须删除memeory viz删除操作符和
删除[]。
为什么不能删除使用删除[]。
删除操作符[]如何知道要调用析构函数的对象的数量。
why does c++ have to operators for deleting memeory viz delete and
delete[].
why cannnot delete be used insted of delete[].
how does the delete operator[] knows the number of object for which
destructor has
to be called.



您好,您可以在FAQ中找到答案:

http://www.parashift.com/c++-faq-lit...html#faq-16.11


希望这有帮助,

-shez-


Hi, you can get the answer in the FAQ:

http://www.parashift.com/c++-faq-lit...html#faq-16.11

Hope this helps,
-shez-


2006年1月5日03:48:36 -0800 in comp.lang .c ++,Shezan Baig

< sh ************ @ gmail.com>写道,
On 5 Jan 2006 03:48:36 -0800 in comp.lang.c++, "Shezan Baig"
<sh************@gmail.com> wrote,
ra ************ @ gmail.com写道:
ra************@gmail.com wrote:
为什么c ++必须向运营商删除memeory viz delete和
删除[]。
为什么要删除删除后使用删除[]。
删除操作符[]如何知道析构函数具有的对象数
被叫。
why does c++ have to operators for deleting memeory viz delete and
delete[].
why cannnot delete be used insted of delete[].
how does the delete operator[] knows the number of object for which
destructor has
to be called.



您好,您可以在常见问题解答中找到答案:

http://www.parashift.com/c++-faq-lit...html#faq-16.11




实际上并没有回答OP的问题。


C ++同时删除和删除[],因为这是标准

所要求的。需要使用数组new []来保存信息所以

delete []知道分配了多少元素。非阵列新的

不需要保存该信息,因此可能有一些值得节省的时间或内存。否则,语言可以只用一个删除操作符设计





Doesn''t actually answer the OP''s question.

C++ has both delete and delete[] because that''s what the standard
calls for. Array new[] is required to save somewhere the info so
that delete[] knows how many elements were allocated. Non-array new
does not need to save that info, so perhaps there might be some
worthwhile saving of time or memory. Otherwise, the language could
have been designed with just one delete operator.


>来自 http://www.scs.stanford.edu/~dm /home.../c++-new.html:


为什么运算符删除和删除[]不同?首先,给定一个类型为foo *的指针

,编译器无法知道它是否指向一个

foo或它们的数组 - 简而言之,编译器不知道哪个内存分配器用于分配foo指向的内存。

其次,当数组的元素有析构函数时,编译器

必须记住数组的大小,以便销毁每个元素。

即使数组元素没有析构函数,每类运算符

删除功能可能依赖于被告知释放的确切大小。对于非数组类型,编译器可以从

重建大小,即释放指针的类型(在删除

超类型的情况下,编译器获得实际值来自虚函数的大小

表,如果有虚拟析构函数)。因此,在分配一个

数组时,编译器有时必须在某处隐藏它的长度;运营商

delete []检索执行必要销毁的长度和

释放。

>From http://www.scs.stanford.edu/~dm/home.../c++-new.html:

Why are operator delete and delete[] different? First, given a pointer
of type foo *, the compiler has no way of knowing if it points to one
foo or an array of them--in short, the compiler doesn''t know which
memory allocator was used to allocate the memory pointed to by foo.
Second, when the elements of an array have destructors, the compiler
must remember the size of the array so as to destroy every element.
Even when array elements don''t have destructors, the per-class operator
delete functions may rely on being told the exact size of what is being
freed. For non-array types, the compiler can reconstruct the size from
the type of the pointer being freed (in the case of deleting a
supertype, the compiler gets the actual size from the virtual function
table if there is a virtual destructor). Thus, when allocating an
array, the compiler must sometimes stash its length somewhere; operator
delete[] retrieves the length to perform the necessary destruction and
deallocation.


这篇关于关于删除[]的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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