First, let's look at memory :

C Language use malloc/free Dynamically manage memory space ,C++ Introduced new/delete,new[]/delete[] To dynamically manage memory .
introduce new/delete,new[]/delete[] Let's get to know operator new,operator delete,operator
new[],operator delete[] function .

notes : These functions are not overloaded new/delete expression .
The function is declared as follows : void* operator new(size_t size); void operator delete(size_t size);
void* operator new[](size_t size); void operator delete[](size_t size); Analysis :
operator new/operator delete,operator new[]/operator delete[] Is a standard library function , Usage and malloc/
free The same way , Distribution only / Free up space , But actually operator new/operator delete just malloc/free One layer encapsulation of .
new/delete: Dynamic management object ;
new[]/delete[] Dynamic management object array .
int* ptr1=new int;// Dynamic allocation 4 Bytes of space delete ptr; int* ptr2=new int(4);
// Dynamic memory allocation 4 Byte space and initialize delete ptr2; int* ptr3=new int[4];// Dynamic memory allocation 16 Byte space delete[];
1>,new/delete What did you actually do ??
new: Call first operator new Allocate space , Call constructor initialization space again .
delete: Call destructor to clean up the object first , Recall operator delete Free up space .

2>,new[]/delete[] What did you actually do ??
new[n]: call operator new Allocate space , Recall n Secondary constructor initialization object .
delete[n]: call n Secondary destructor cleaning up objects , Recall operator delete Free space .

Why does the compiler know how many constructors to call , The destructor ?
Originally in new[ ] Allocate more space in the head 4 Bytes to store n, This is called new[]/delete[] You know how many times to call constructors and destructors .

new/delete,new[]/delete[] Why do you want to appear in pairs ?
When new When creating a built-in type of space , It's OK to be out of pairs ; But when you open a non built-in type space , We need to open up more 4 Bytes , At this time, if not used in pairs, it will cause memory leak or program crash .

Implementation with macro simulation new[]/delete[] Request and release array
//DELETE_ARRAY Parameter in transit n #define NEW_ARRAY(ptr,type,n) do{ ptr=(type*)operatornew(
sizeof(type)*n); for (size_t i = 0; i <n;++i) { new(ptr+i)type; } } while (0);
#define DELETE_ARRAY(ptr,type,n) do{ for (size_t i = 0; i < n; ++i) {
(ptr+i)->~String(); } operator delete ptr; }while (0); // to DELETE_ARRAY No transmission in the middle n #
define NEW_ARRAY(ptr,type,n) do{ ptr = (type*)operator new(sizeof(type)*n + 4);
// to n Also allocate space *(int*)ptr = n; ptr=(type*)((char*)ptr+4); for (size_t i = 0; i < n;
++i) {new(ptr + i)(type); } } while (0); #define DELETE_ARRAY(ptr,type) do{
size_t n = *((int*)ptr - 1); for (size_t i = 0; i < n; ++i) { (ptr +
i)->~String(); }operator delete(char*(ptr - 4)); } while (0);
malloc/free and new/delete Relationships and differences between
relationship : Dynamic memory management .
difference :1>,malloc/free Is a standard library function , new/delete Is operator ;
2>,malloc/free Just distribution / Free memory ,new/delete Not only distribution / Freeing memory also calls constructor initialization and destructor cleanup ;
3>,malloc/free Calculate type size manually , Return value void*,new/delete Calculate type size automatically , Return pointer of corresponding type ;
4>,malloc/free Failed return 0, new/delete Failure throw exception .

©2019-2020 Toolsou All rights reserved,
java Four functional interfaces ( a key , simple )os Simple use of module Browser kernel ( understand ) Some East 14 Pay change 16 salary , Sincerity or routine ?HashMap Explain in detail It's unexpected Python Cherry tree (turtle The gorgeous style of Library )html Writing about cherry trees , Writing about cherry trees