>Does using the keyword "new" automatically put the object on the heap?
Yes.
>Before function ends, I have to delete pointer to deallocate memory used by object?
You are not deleting the pointer. You are telling the heap manager to free the memory block referenced by the pointer. The pointer itself is often allocated on the stack. Also make sure you do the appropriate delete for the type of new.
From C++ Primer Plus, Third Edition by Stephen Prata, Copyright 1998 by Sams publishing:
Don't use delete to free memory that new didn't allocate.
Don't use delete to free the same block of memory twice in succession.
Use delete [] if you used new [] to allocate an array.
Use delete (no brackets) if you used new to allocate a single entity.
It is safe to apply delete to the null pointer (nothing happens).
>If I didn't, would it automatically be deleted when object reference goes out of scope when function ends?
No, only objects created on the stack are automatically deleted when the object reference goes out of scope.
Previous
Reply
View the map of this thread
View the map of this thread starting from this message only
View all messages of this thread
View all messages of this thread starting from this message only