This is the sixth day of my participation in the August More text Challenge. For details, see:August is more challenging
Preface:
C allocates and frees dynamic memory by malloc and free respectively. Malloc is used to allocate heap memory, and free is used to free heap memory.
C++ defines two operators to allocate and free dynamic memory. The operator new allocates heap memory and the operator delete frees new allocated heap memory.
1, new and delete usage brief introduction
New:
The end parenthesis () is optional. Dynamically allocated objects are initialized by default. The default constructor is called,() is used for initialization.
Vector is a generic class template, and vector<int> is a concrete type just like basic_string is a string
double *p = new double;
double *pp = new double(3.14);
vector<int> *ppp = new vector<int> {1.2.3};
Copy the code
If the number of elements in the list is less than size, the first N initializers are initialized. If the number of elements in the list is larger than size, the first N initializers are initialized. If the number of elements in the list is larger than size, the first N initializers are initialized. The type is TypeName, for example:
double *p = new double[10];
double *pp = new double[10] {3.14};
vector<int> *ppp = new vector<int> [2] {{1.2.3}, {1.2.3}};
Copy the code
Details of the new:
By default, dynamically allocated objects are initialized by default, and the default constructor is called.
Usage 1: () call the corresponding constructor;
() can be used with the syntax sugar auto, auto will infer the type in (), but can only have a single initializer;
Detail 4: Dynamically allocated const objects must be explicitly initialized. A type with a default constructor can be implicitly initialized.
Detail 5: When memory runs out, an exception is thrown. When allocation fails, a null pointer is returned.
Detail 6: Dynamically allocating an empty array is legal;
Delete:
Release new allocated object memory, delete obj_pointer, release new allocated array, delete [] obj_pointer
Details: obj_pointer must point to new dynamically allocated memory or a null pointer. Delete calls the destructor
2, new and DELETE allocate heap memory details process
Details of the new process:
Example:
class AA
{
public:
AA(int x,int y){}
};
AA *pc=new AA(1.1);
Copy the code
AA * PC = new AA (1, 1); It turns into three processes:
Void * mem = operator new (sizeof (AA)); Operator new () will call malloc (n) internally, where n=sizeof (AA);
PC = static_cast<AA*> MEm cast; Convert the resulting generic pointer to a pointer of type AA;
PC ->AA::AA(1,1); Call constructor to initialize AA;
Delete details procedure:
Example:
delete AA;
Copy the code
The delete process is simple:
Step 1: AA::~AA(PC); Call the destructor;
Operator delete(PC); Operator delete() calls free(), which is free(PC);
Why should array New be paired with Array Delete?
Array new allocates memory that is not equal to size*sizeof(class) at the top of the array. In 32-bit computers, there are cookies at the top of the array. They count the sizeof the array. If the memory allocated by array New is freed with a delete, the memory will be freed exactly according to the count, but the destructor will be called only once. If the destructor contains a delete of another type, then the memory leak will occur, because only the destructor for the first object in the array is called. Only the delete in the first destructor will be freed, the rest will cause a memory leak!!
That’s all for today’s article, welcome your criticism and correction;