Let's say I have a temp pointer to a Student object (doesn't matter what the definition of Student is - let's say it contains no pointer data members), which I'm using as follows :
int main(){
Student *temp;
Student *s1 = new s1("John", 21);
Student *s2 = new s2("Jane", 23);
//do whatever;
temp = s1;
s1 = s2;
s2 = temp;
delete s1;
delete s2;
return 0;
}
Since I used new
for s1
and s2
, I know the objects are created in the heap, and I need to use the delete
operator to get rid of them. My question is - do I need to "delete temp"? If not, why? How is "temp's" memory managed? Could someone explain how the memory manager handles this pointer, and what happens to it when it goes out of scope?