First, the new operator, delete operator
class String { public: String(const char *str="") { if(str== NULL) { data=new char[1]; data='\0'; } else { data=new char[strlen(strlen(str)+1]; strcpy(data,str); } ~String() { delete[] data; data=NULL; } private: char *data; };
In the above String class, when you define a String object, use new to create an object, and use delete to destruct the object, then new
There are two operations with delete, respectively, when creating an object with new, the first step is to open up memory space, and the second step is to use the constructor to construct the pair
elephant. Similarly, when using delete to destruct an object, there are also two operations. The first step is to call the destructor function to destruct the object, and the second step is to release its memory.
space. When an object is created with new and when it is destructed with delete, the order is reversed.
2. Operator new, operator delete
void* operator new(size_t sz) { void *p= malloc(sz); return p; } void operator delete(void *p) { free(p); } void* operator new[](size_t sz) { void *p= malloc(sz); return p; } void operator delete[](void *p) { free(p); }
When you create an object, the new operator will include two-step operations. The first step is to call the function of the overloaded operator new above, and then open up
Memory space, the second step is to call the constructor of the class to construct the object. When destructing an object, the delete operator also consists of two steps, the first
The first step is to call the destructor of the class to destruct the object, and the second step is to call the function of the overloaded operator delete above to release the memory space. Likewise, this
It is a function to create a single object with new. When you need to dynamically open up the array space, the following two functions are the function to open up the array space and release the array space.
function of storage space. It should be noted that void* operator new(size_t sz); the parameter type of the function must be size_t type, that is, unsigned integer
Number type, and the return value of the function must be void* type, otherwise the compilation will fail, and the following function to open up the array space is the same.
3. Positioning new
void* operator new(size_t sz) { void *p= malloc(sz); return p; } void operator delete(void *p) { free(p); } void* operator new[](size_t sz) { void *p= malloc(sz); return p; } void operator delete[](void *p) { free(p); } class String { public: String(const char *str="") { if(str== NULL) { data=new char[1]; data='\0'; } else { data=new char[strlen(strlen(str)+1]; strcpy(data,str); } ~String() { delete[] data; data=NULL; } private: char *data; }; void* operator new(size_t sz,int *d,int pos) { return &d[pos]; } intmain () { String *ps= (String*)operator new(sizeof(String)); new(ps)String("Hello"); ps ->~ String(); operator delete (ps); // new(p) type (initial value) int ar[ 10 ]; new (ar, 3 ) int ( 10 ); new (ar, 5 ) int ( 99 ); new (ar, 9 ) int ( 9 ); return 0 ; }
Positioning new means overloading operator new. In the overloaded function, you need to use an extra pointer to point to the created array space, pos is
The subscript position in the array is to insert the required data into the specified pos position, which is positioning new.