using multiple new() statement on same pointer

I am curious about the new operator. if i have the statements

1
2
int *p = new int(4);
p = new int(3);


What happens in the memory? Does the memory for 4 get deallocated or does 4 get replaced by 3?

Thanks
Harsh.
memory leak

the first int remains allocated and you will never be able of deleting it as you lost its address
Hi Bazzy,

Thank you for your quick reply. So if I have an infinite loop

1
2
3
4
5
int *p;
while(1)
{
     p = new int(3);
}


Will the above code will continue running till the system runs out of RAM and then crash?
Try it for yourself and see.
...which is about the same thing.

Modern OSs recognize this kind of behavior as foolish and will terminate your program before letting the OS crash.

However, the end result is still that your program will crash.
Topic archived. No new replies allowed.