Is delete[] equal to delete?

前端 未结 6 1591
-上瘾入骨i
-上瘾入骨i 2020-11-22 01:05
IP_ADAPTER_INFO *ptr=new IP_ADAPTER_INFO[100];

if I free using

delete ptr;

will it lead to memory leak, if not t

6条回答
  •  死守一世寂寞
    2020-11-22 01:42

    For array of POD it will not leak (with the most compilers). For example, MSVC generates identical code for delete and delete[] for array of POD.

    Personally, I think C/C++ could be without operator delete[]. Compiler knows object size and allocated memory size is known at runtime, thus it is very simple to know is a pointer array or not and dispose memory in a right way.

    EDIT:

    OK, guys. Can you test at your compiler and say whether it leak?

    Try to think as a compiler developer. We have new, new[], delete, delete[]. Each new has its own delete. Seems perfect and complete. Let's see what is going on when you call delete[]?

    1. call vector destructor for an object
    2. actual free memory
    

    What is destructor for POD? Nothing! So, calling delete for array of POD will not leak! Even if it breaks the standard. Even if it is not recommended.

    EDIT2:

    This is disassembly code generated by VS2008:

    operator delete[]:
    78583BC3  mov         edi,edi 
    78583BC5  push        ebp  
    78583BC6  mov         ebp,esp 
    78583BC8  pop         ebp  
    78583BC9  jmp         operator delete (78583BA3h) 
    

提交回复
热议问题