Clarification on dynamic memory
If I have the following piece of code
int *q = new int(42), *r = new int(100);
r = q;
r will now point to the same dynamically allocated object as q. What
happens to the object that was created during the initialization of r? Is
it still valid? How do you delete it?
I know that writing
delete q;
will invalidate both q and r. If I did
delete r;
won't this invalidate both q and r also?
And even if it did wouldn't the original int object with value 100 still
be hanging around?
No comments:
Post a Comment