Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is delete[] equal to delete?

IP_ADAPTER_INFO *ptr=new IP_ADAPTER_INFO[100]; 

if I free using

delete ptr; 

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

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  
like image 813
Satbir Avatar asked Oct 12 '09 08:10

Satbir


People also ask

How delete [] is different from delete?

delete is used for one single pointer and delete[] is used for deleting an array through a pointer.

How does delete [] know how much to delete?

new[] also stores the number of elements it created in the memory block (independently of malloc ), so that later delete[] can retrieve and use that number to call the proper number of destructors.

What is the effect of delete []?

However, delete[] is designed to call destructors for every object allocated, whereas delete would only call it on the first object, i.e. a[0]. This is why the memory leak happens.

What does delete [] mean in C++?

Delete is an operator that is used to destroy array and non-array(pointer) objects which are created by new expression. Delete can be used by either using Delete operator or Delete [ ] operator. New operator is used for dynamic memory allocation which puts variables on heap memory.


2 Answers

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:

Just stay away from it.

like image 117
sbi Avatar answered Oct 08 '22 02:10

sbi


Just an illustration of some "undefined" behaviors on certain OSes and compilers. Hope it could be helpful for people to debug their code.

Test 1

#include <iostream> using namespace std; int main() {   int *p = new int[5];   cout << "pass" << endl;   delete p;   return 0; } 

Test 2

#include <iostream> using namespace std; int main() {   int *p = new int;   cout << "pass" << endl;   delete[] p;   return 0; } 

Test 3

#include <iostream> using namespace std; struct C {   C() { cout << "construct" << endl; }   ~C() { cout << "destroy" << endl; } };  int main() {   C *p = new C[5];   cout << "pass" << endl;   delete p;   return 0; } 

Test 4

#include <iostream> using namespace std; struct C {   C() { cout << "construct" << endl; }   ~C() { cout << "destroy" << endl; } };  int main() {   C *p = new C;   cout << "pass" << endl;   delete[] p;   return 0; } 
  • Windows 7 x86, msvc 2010. Compile with default options, i.e. exception handler is enabled.

Test 1

pass 

Test 2

pass 

Test 3

construct construct construct construct construct pass destroy # Then, pop up crash msg 

Test 4

construct pass destroy destroy destroy destroy destroy destroy destroy ... # It never stop until CTRL+C 
  • Mac OS X 10.8.5, llvm-gcc 4.2 or gcc-4.8 generate the same output

Test 1

pass 

Test 2

pass 

Test 3

construct construct construct construct construct pass destroy a.out(71111) malloc: *** error for object 0x7f99c94000e8: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug zsh: abort      ./a.out 

Test 4

construct pass a.out(71035) malloc: *** error for object 0x7f83c14000d8: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug zsh: abort      ./a.out 
  • Ubuntu 12.04, AMD64, gcc 4.7

Test 1

pass 

Test 2

pass 

Test 3

construct construct construct construct construct *** glibc detected *** ./a.out: munmap_chunk(): invalid pointer: 0x0000000001f10018 *** ======= Backtrace: ========= /lib/x86_64-linux-gnu/libc.so.6(+0x7eb96)[0x7fe81d878b96] ./a.out[0x400a5b] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7fe81d81b76d] ./a.out[0x4008d9] ======= Memory map: ======== .... zsh: abort (core dumped)  ./a.out 

Test 4

construct destroy destroy destroy destroy destroy destroy destroy destroy ... destroy destroy *** glibc detected *** ./a.out: free(): invalid pointer: 0x00000000016f6008 *** ======= Backtrace: ========= /lib/x86_64-linux-gnu/libc.so.6(+0x7eb96)[0x7fa9001fab96] ./a.out[0x400a18] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7fa90019d76d] ./a.out[0x4008d9] ======= Memory map: ======== ... zsh: abort (core dumped)  ./a.out 
like image 45
jichi Avatar answered Oct 08 '22 04:10

jichi