delete[] 等于 delete 吗? [英] Is delete[] equal to delete?

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

问题描述

IP_ADAPTER_INFO *ptr=new IP_ADAPTER_INFO[100];

如果我免费使用

delete ptr;

会不会导致内存泄漏,如果不会,那是为什么?

will it lead to memory leak, if not then why ?

这是VS2005生成的反汇编代码

This is disassembly code generated by VS2005

; delete ptr;
0041351D  mov         eax,dword ptr [ptr] 
00413520  mov         dword ptr [ebp-0ECh],eax 
00413526  mov         ecx,dword ptr [ebp-0ECh] 
0041352C  push        ecx  
0041352D  call        operator delete (4111DBh) 
00413532  add         esp,4 

; delete []ptr;
00413535  mov         eax,dword ptr [ptr] 
00413538  mov         dword ptr [ebp-0E0h],eax 
0041353E  mov         ecx,dword ptr [ebp-0E0h] 
00413544  push        ecx  
00413545  call        operator delete[] (4111E5h) 
0041354A  add         esp,4 

推荐答案

这是否会导致内存泄漏、擦除您的硬盘、让您怀孕、让讨厌的 Nasal Demons 在您的公寓周围追逐您,或者让一切正常没有明显的问题,是未定义的.一个编译器可能是这样,另一个编译器会改变,随着新编译器版本的变化,随着每个新的编译,月相,你的心情,或者取决于最后一个晴天时通过处理器的中微子数量下午.或者它可能不会.

Whether this leads to a memory leak, wipes your hard disk, gets you pregnant, makes nasty Nasal Demons chasing you around your apartment, or lets everything work fine with no apparent problems, is undefined. It might be this way with one compiler, and change with another, change with a new compiler version, with each new compilation, with the moon phases, your mood, or depending on the number of neutrinos that passed through the processor on the last sunny afternoon. Or it might not.

所有这些,以及无数其他的可能性都被放在一个术语中:未定义行为:

All that, and an infinite number of other possibilities are put into one term: Undefined behavior:

远离它.

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

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