1 //dynamically allocating storage space using new2 3#include <iostream>4 usingstd::cout;5 6 intMain ()7 {8 //1th Way9 int*a=New int;Ten*a=1; Onecout<<"the result of dynamically allocating storage space using the first method is: \ n" A<<"*a="<<*a<<Std::endl; -Delete A;//releasing dynamic storage space - //2nd Way the int*b=New int(2); -cout<<"the result of dynamically allocating storage space using the first method is: \ n" -<<"*b="<<*b<<Std::endl; -Delete B;//releasing dynamic storage space + //3rd Way - int*C; +C=New int(3); Acout<<"the result of dynamically allocating storage space using the first method is: \ n" at<<"*c="<<*c<<Std::endl; -Delete C;//releasing dynamic storage space - - //Creating arrays Dynamically - float*d=New float[3]; -d[0]=3; ind[1]=6; -d[2]=8; tocout<<"d[0]="<<d[0]<<Std::endl; +D=d+1; The fundamental difference between an array name and a pointer -cout<<"d[0]="<<d[0]<<Std::endl; thed=d-1; *cout<<"d[0]="<<d[0]<<Std::endl; $delete [] D;//releasing dynamic storage spacePanax Notoginseng return 0; -}
Use new in C + + to dynamically generate an array space and use Delete to release storage space