The new and delete operators are used to dynamically allocate and revoke memory.
New usage:
1. Open a single variable address space
1) New int; // open up a bucket for storing arrays and return an address pointing to the bucket. int * A = new INT: assign an int type address to integer pointer.
2) int * A = new int (5) serves the same purpose as above, but at the same time, the integer is assigned to 5
2. Open up array space
One-dimensional: int * A = new int [100]; opens up an integer array space of 100
Two-dimensional: int ** A = new int [5] [6]
3D and above: so on.
General Usage: New Type [initial value]
Delete usage:
1. int * A = new int;
Delete A; // release the space of a single int
2. int * A = new int [5];
Delete [] A; // release the int array space
To access the struct space opened by new, the variable name cannot be used directly, but can only be accessed through the pointer of the value assignment.
New and delete can be used to dynamically open up and cancel the address space. when compiling a Program , if you use a variable (usually an array temporarily stored), you need to use it again next time, but I want to save the effort of re-initialization. I can open up a space every time I start using it and undo it after I use it.