C ++ ---应该是new / new []匹配delete / delete []? [英] C++ --- Should new/new[] match delete/delete[]?

查看:170
本文介绍了C ++ ---应该是new / new []匹配delete / delete []?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道当我们使用new / new []分配内存时,我们应该分别使用delete / delete []释放内存。

I knew that when we allocate memory by using new/new[], then we should release the memory by using the delete/delete[] respectively.

是问题,

我可以使用delete []释放新分配的内存吗?

Can I use delete[] to release the memory allocated by new?

int *pInt3 = new int;
delete [] pInt3;
pInt3 = NULL;

T *pT3 = new T;
delete [] pT3;
pT3 = NULL;

谢谢

推荐答案

否,您应该将 new 的非数组形式与非数组 delete

No, you should match the non-array form of new with non-array delete, and likewise for the array forms.

请参阅 Freestore管理的C ++常见问题解答部分

这是特别好的: http://www.parashift.com/c%2B%2B-faq -lite / freestore-mgmt.html#faq-16.12


[16.12]如果我在删除时忘记了[通过新的T [n]分配的数组?

[16.12] What if I forget the [] when deleteing array allocated via new T[n]?

所有生命都是灾难性的结束。

All life comes to a catastrophic end.

程序员不是编译器负责获得新T [n]和删除[] p之间的连接正确。如果错误,编译器不会生成编译时错误消息和运行时错误消息。堆腐败是可能的结果。或更糟。您的程序可能会死亡。

It is the programmer's —not the compiler's— responsibility to get the connection between new T[n] and delete[] p correct. If you get it wrong, neither a compile-time nor a run-time error message will be generated by the compiler. Heap corruption is a likely result. Or worse. Your program will probably die.

这篇关于C ++ ---应该是new / new []匹配delete / delete []?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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