how does the delete operator work? Is it better then free()?. Also if u do ptr=new char[10], then delete using delete ptr, how does the pointer know how many locations to delete.
how does the delete operator work? Is it better then free()?. Also if u
Share
There is only
deleteoperator,freeexist only as function. Under C++, you are encouraged to usenew/deleteovermalloc()/free().There is an internal “magic” in delete operator. When an array is created with
new[], the size of array is stored in the metadata at the memory block.delete[]makes use of that information.All this is of course compiler-, OS-, optimizer- and implementation-dependent.