mem lack


Hi all,

I am a bit confused about the real cause of memory leak in this case:

int a(4);
int Pointer_1*;
int Pointer_2* = &a

Pointer_1 = new int;

Pointer_1 = Pointer_2;

I know that the Pointer_1 is in this way overwritten and the pointer to the allocated int is lost. But i still can't understand why it leads to mem lack if the pointer to the allocated object is lost?

Thanks in advance
The only pointer to the piece of memory allocated to Pointer_1 has been overwritten.

That means there is no way to call delete on the new memory that was allocated.

And if you can't call delete on that allocated memory it will no longer be available to the rest of the program.

Every time that piece of code executes during the running of the program another piece of memory will be allocated but not deallocated the same way.

If the code is executed often then it can eat up large amounts of memory.
Last edited on
Topic archived. No new replies allowed.