Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ Programming
➜ STL
➜ Auto pointers
It is now over 60 days since the last post. This thread is closed.
Refresh page
| Posted by
| Nick Gammon
Australia (23,173 posts) Bio
Forum Administrator |
| Date
| Fri 04 Jul 2003 02:13 AM (UTC) Amended on Fri 04 Jul 2003 02:20 AM (UTC) by Nick Gammon
|
| Message
| Here is a cute thing I found whilst reading the STL books - auto pointers. Basically they are supposed to solve the problem of doing a new and forgetting to delete the resulting memory.
This can happen more easily than you may think if a function throws an exception.
Below is an example of using them. Be warned that the MS VC library and the gcc library seem to handle auto_ptr a bit differently. The MS STL library simply transfers ownership of the pointer (so you can use it after assigning it) whilst the gcc STL library actually sets the original pointer to NULL (thus, you can't use it after assigning it). I would be very careful about assigning auto pointers because of this.
Also, I note from the STL code that the destructor deletes the pointer, it doesn't allow for an array. What I mean is that if you do this:
int * i = new int [100];
You are supposed to delete it like this:
delete [] i;
Since the auto_ptr doesn't know to do that, this will not work properly:
auto_ptr<int> i (new int [100]);
However I should point out that if you are using STL in the first place, you should probably be using a vector instead of an array of ints in the first place. ;)
Note also you don't delete an auto_ptr - you simply let it delete itself in the destructor when it goes out of scope.
#include <iostream>
#include <memory>
#include <string>
#include <stdexcept>
using namespace std;
class Widget
{
public:
Widget () { cout << "widget constructor" << endl; };
~Widget () { cout << "widget destructor" << endl; };
void test () { cout << "widget test" << endl; }
};
// output operator for auto_ptr (see Josuttis p 47)
template <class T>
ostream& operator<< (ostream& strm, const auto_ptr<T>& p)
{
if (p.get () == NULL)
strm << "NULL";
else
strm << p.get ();
return strm;
} // end of ostream& operator<<
void sub (void)
{
auto_ptr<Widget> w (new Widget);
auto_ptr<Widget> x; // NULL pointer at this time
w->test (); // test it (use like a normal pointer)
cout << "Before assignment..." << endl;
cout << " w: " << w << endl;
cout << " x: " << x << endl;
// transfer ownership of pointer from w to x
x = w;
x->test (); // test it again
cout << "After assignment..." << endl;
cout << " w: " << w << endl;
cout << " x: " << x << endl;
throw runtime_error ("Trouble brewing");
}
int main (void)
{
try
{
cout << "Before sub" << endl;
sub ();
cout << "After sub" << endl;
}
catch (runtime_error & e)
{
cout << "Exception: " << e.what () << endl;
}
return 0;
} // end of main
Output on Linux
Before sub
widget constructor
widget test
Before assignment...
w: 0x804a880
x: NULL
widget test
After assignment...
w: NULL
x: 0x804a880
widget destructor
Exception: Trouble brewing
Output on MS VC++
Before sub
widget constructor
widget test
Before assignment...
w: 00300030
x: NULL
widget test
After assignment...
w: 00300030
x: 00300030
widget destructor
Exception: Trouble brewing
Notice the difference, after the assignment the pointer still exists (is not NULL) in this case.
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
12,122 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top