c ++ new and delete

  1. Open up a single variable address space
  
  1) new int; // Open up a storage space to store an array and return an address pointing to the storage space. Int * a = new int is to assign an address of type int to an integer pointer a.
  
  2) int * a = new int (5) Same as above, but at the same time assign the integer to 5
  
  2. Open up
  
  one-dimensional array space : int * a = new int [100]; open up an integer array of size 100 Space
  
  2D: int ** a = new int [5] [6]
  
  3D and above: and 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 space of the int array
  
  To access the structure space created by new, it cannot be done directly by the variable name, only The assigned pointer is used for access.
  
  New and delete can be used to dynamically open up and undo the address space. If you run out of a variable (usually an temporarily stored array) when programming, you need to use it next time, but you want to save it For the initial effort, you can open up a space each time you start using it, and undo it after you use it.

Guess you like

Origin www.cnblogs.com/zhenhua1618/p/12729268.html