Week 1: Dynamic memory allocation

new operator to implement dynamic memory allocation

method 1: assign a variableP = 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 arrayP = 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

Guess you like

Origin blog.csdn.net/ZmJ6666/article/details/108548295