Managing Memory in C++ - Realeasing memory (Deallocation)
One final point that we need to make in your use of pointers and dynamic memory allocation is releasing memory. Each time you allocate memory in your application, it is reserved by the operating system so that other applications cannot access that memory address. This has security implications as well as importance for separation of application code to prevent system wide crashes if an errant application behaves badly.
If you do not release the memory in your application, the operating system will not reclaim it and this is known as a memory leak. It is compounded if your application continues to dynamically allocate memory and doesn't release it.
The simple way of releasing your allocated memory is to use the keyword delete. For the sample code above, you would make use of the delete keyword as demonstrated here.
We have simply issued the delete keyword in reference to each pointer we have in the application code. The operating system will now reclaim the memory used by those pointers and our code no longer has a memory leak. Use whatever method makes sense to you to ensure that memory is released like using a delete keyword for every new keyword.
Last updated