Use SmartPointers to avoid Memory leak issues.
No, you don't need to set the Pointer variable to "NULL", the program will run without issues.
Setting it to null can act as extra security to prevent usage of the variable by mistake.
-----------------------
Operator new reserves space so that any new memory allocation or for internal operations the compiler does not occupy those allocated memory locations.
Operator Delete just un-marks those reservations. The reference variable still has the address of the memory location and you can access it but now, it can be altered any time by the compiler.
Contents of the Pointer variable before and after delete Operation.
int *a;
cout<<"before memory allocation a= "<<a<<endl;
a= new int[10];
a[0]= 1; a[7]=2;
cout<<"After memory allocation but Before Delete"<<endl;
cout<<"a="<<a<<"\ta[0]="<<a[0]<<"\ta[7]="<<a[7]<<"\ta[22]="<<a[22]<<endl;
cout<<"sizeof(a)="<<sizeof(a)<<"\tsizeof(a[7])="<<sizeof(a[7])<<"\tsizeof(a[22])="<<sizeof(a[22])<<endl;
delete[] a;
cout<<"After Delete"<<endl;
cout<<"a="<<a<<"\ta[0]="<<a[0]<<"\ta[7]="<<a[7]<<"\ta[22]="<<a[22]<<endl;
cout<<"sizeof(a)="<<sizeof(a)<<"\tsizeof(a[7])="<<sizeof(a[7])<<"\tsizeof(a[22])="<<sizeof(a[22])<<endl;
return 0;
Result:
before memory allocation a= 0x40ed39
Before Delete
a=0x9a1550 a[0]=1 a[7]=2 a[22]=1316552972
sizeof(a)=8 sizeof(a[7])=4 sizeof(a[22])=4
After Delete
a=0x9a1550 a[0]=10099280 a[7]=2 a[22]=1316552972
sizeof(a)=8 sizeof(a[7])=4 sizeof(a[22])=4