new operator to implement dynamic memory allocation
method 1: assign a variable
P = new T
- P is a T*, T is any type, dynamically allocate a memory space of sizeof(T), and then save the starting address in P
method 2: allocate an array
P = new T[N]
- N is the number of elements of the array to be allocated, allocate a piece of memory with a memory size of n*sizeof(T), and assign the starting address to P
new one, delete one
delete 指针
, The pointer must point to the new space, and the same place cannot be deleted twice
delete [] 指针
, delete the array