If you declare a variable in a function
Code:
void myfunc (){
* * *int a = 33;
}
that variable (in this case [tt]a[/tt]) only exists throughout the duration of the function and is then wiped off the stack.
I assume it works the same way with objects
Code:
void myfunc(){
* * *MyObject obj;
}
[tt]obj[/tt] only exists on the stack throughout the function.
My question is what happens here:
Code:
void myfunc(){
* * *MyObject *obj = new MyObject;
}
The [tt]new[/tt] operator creates the object on the heap, so the object remains after the function is over, right? *But wouldn't the pointer to that object be removed along with all other local variables when the function ends? *So this object would be a memory leak, right? *
Bookmarks