Any block of memory allocated with new
is unavailable until it is released, either by a corresponding delete
or when the process in which the memory was allocated terminates.
The memory allocator actually has no clue about what references you maintain on allocated objects.It just allows you to release the corresponding memory blocks if you manage to provide valid addresses.
How you keep track of these addresses is entirely up to you.
So if for some reason you loose track of a chunk of memory (by overwriting a data pointer as in your example, or simply forgetting to use it at some point to release the memory), you will deprive your system of this bit of memory until the underlying process terminates.
This becomes a memory leak if the "memory lapse" is unintentional, even more so if this occurs repeatedly.
For instance, if you need some data that has the same life span as the process, you can do a one-time allocation and never release the memory (since it will be released automatically upon termination). That could horrify the good practices zealots, but would nevertheless not qualify as a memory leak.
On the other hand, there are other ways of messing up you memory allocator : passing an invalid pointer to delete
(including a reference to an already released object, i.e. deleting the same instance of an object more than once, or a pointer to something that was not allocated by new
, like a local or global variable) will bring you the wonderful realm of undefined behaviours, which could cause random memory losses, data corruption or nuclear meltdowns.