auto_ptr - passing to functions and best practices
-
void anotherMethod(auto_ptr<Foo> foo)
{
cout << "Taken ownership. " << foo->getAString() << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
auto_ptr<Foo> foo(new Foo);
cout << foo->getAString() << endl;
anotherMethod(foo);
// this will throw error as the pointer is deleted
cout << "From main again " << foo->getAString();
cout << "Ending main" << endl;
return 0;
}In the avove code, I am trying to pass the
auto_ptr
to another function. But doing this will make the another function take ownership and the underlying pointer will be deleted when another functions scope ends. So I am unable to access it in themain
method again. To workaround this issue, I changed the code likevoid anotherMethod(const auto_ptr<Foo>& foo)
{
cout << "Taken ownership. " << foo->getAString() << endl;
}The below code will also work
void anotherMethod(Foo* foo)
{
cout << "Taken ownership. " << foo->getAString() << endl;
}// in main I called like
auto_ptr<Foo> foo(new Foo);
anotherMethod(foo.get());I am wondering which one is the best approach? Can anyone help? Also is it a good practice to use
auto_ptr
over normal pointers and is it always guranteed the cleanup? -
void anotherMethod(auto_ptr<Foo> foo)
{
cout << "Taken ownership. " << foo->getAString() << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
auto_ptr<Foo> foo(new Foo);
cout << foo->getAString() << endl;
anotherMethod(foo);
// this will throw error as the pointer is deleted
cout << "From main again " << foo->getAString();
cout << "Ending main" << endl;
return 0;
}In the avove code, I am trying to pass the
auto_ptr
to another function. But doing this will make the another function take ownership and the underlying pointer will be deleted when another functions scope ends. So I am unable to access it in themain
method again. To workaround this issue, I changed the code likevoid anotherMethod(const auto_ptr<Foo>& foo)
{
cout << "Taken ownership. " << foo->getAString() << endl;
}The below code will also work
void anotherMethod(Foo* foo)
{
cout << "Taken ownership. " << foo->getAString() << endl;
}// in main I called like
auto_ptr<Foo> foo(new Foo);
anotherMethod(foo.get());I am wondering which one is the best approach? Can anyone help? Also is it a good practice to use
auto_ptr
over normal pointers and is it always guranteed the cleanup?take a look at boost's shared_ptr. it's reference-counted, so you won't run into problems if you pass one to a function, or store one in an STL container.
-
take a look at boost's shared_ptr. it's reference-counted, so you won't run into problems if you pass one to a function, or store one in an STL container.
Classes from "Boost" looks promising. Thanks for the information. BTW, just for academic interest, can you look into the example I have given in the initial post? I just want to make sure
auto_ptr
can be used in such a way. -
Classes from "Boost" looks promising. Thanks for the information. BTW, just for academic interest, can you look into the example I have given in the initial post? I just want to make sure
auto_ptr
can be used in such a way.yes, i think your examples are correct. since the problem with auto_ptr, in this situation, is that passing an auto_ptr to a function makes a copy, and the copy takes control of the allocated memory. passing it by reference (either as a ref or as a ptr) avoids that copy, and the outer function maintains control of the memory.
-
yes, i think your examples are correct. since the problem with auto_ptr, in this situation, is that passing an auto_ptr to a function makes a copy, and the copy takes control of the allocated memory. passing it by reference (either as a ref or as a ptr) avoids that copy, and the outer function maintains control of the memory.
Many thanks :)
-
Classes from "Boost" looks promising. Thanks for the information. BTW, just for academic interest, can you look into the example I have given in the initial post? I just want to make sure
auto_ptr
can be used in such a way.FYI: Visual C++ 2008 SP1+ has a shared_ptr Class[^] as well. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
FYI: Visual C++ 2008 SP1+ has a shared_ptr Class[^] as well. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
Thanks Mark. But unfortunatly I can't use it as my application is portable across multiple platforms. I believe the one which you said is not portable (please correct me if I am wrong). Your help is much appreciated though. :)
-
Thanks Mark. But unfortunatly I can't use it as my application is portable across multiple platforms. I believe the one which you said is not portable (please correct me if I am wrong). Your help is much appreciated though. :)