Say, for example, I have the following code (pure example):
class a {
int * p;
public:
a() {
p = new int;
}
~a() {
delete p;
}
};
a * returnnew() {
a retval;
return(&retval);
}
int main() {
a * foo = returnnew();
return 0;
}
In returnnew (), would retval be destroyed after the function returns (when retval is out of scope)? Or it will disable auto kill after I return the address and I could say delete foo; at the end of main ()? Or in a similar vein (pseudo-code):
void foo(void* arg) {
bar = (a*)arg;
exit_thread();
}
int main() {
while(true) {
a asdf;
create_thread(foo, (void*)&asdf);
}
return 0;
}
where will the destructor go? where should i say delete? or is this behavior undefined? Would it be the only possible solution to use counted STLs? How will this be implemented?
Thank you, I used C ++ for a while, but have not been in this situation and do not want to create memory leaks.