Programming with Passion

Make the best out of everything.

Monday, 21 March 2016

The new Operator && The delete Operator

The new Operator

ptr = new float[n];
This expression in the above program returns a pointer to a section of memory just large enough to hold the n number of floating-point data.

The delete Operator

Once the memory is allocated using new operator, it should released to the operating system. If the program uses large amount of memory using new, system may crash because there will be no memory available for operating system. The following expression returns memory to the operating system.
delete [] ptr;
The brackets [] indicates that, array is deleted. If you need to delete a single object then, you don't need to use brackets.
delete ptr;

No comments:

Post a Comment