Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Magic about auto_ptr !!

Reply
Thread Tools

Magic about auto_ptr !!

 
 
Binary
Guest
Posts: n/a
 
      10-24-2006
Hi,

I am reading a chinese book about STL, the book says below code will
have no memory leak. I think its magic and wonder why the auto_ptr
knows the memory should be freed when leaving the function call:

void func()
{
auto_ptr<string> ps(new string("jjhou"));
cout << *ps << endl;
// leave the function without delte, the auto_ptr will release it
automatically
}

TIA.
ABAI

 
Reply With Quote
 
 
 
 
flagos
Guest
Posts: n/a
 
      10-24-2006
You can find the answer here:

http://www.parashift.com/c++-faq-lite/dtors.html


Binary wrote:
> Hi,
>
> I am reading a chinese book about STL, the book says below code will
> have no memory leak. I think its magic and wonder why the auto_ptr
> knows the memory should be freed when leaving the function call:
>
> void func()
> {
> auto_ptr<string> ps(new string("jjhou"));
> cout << *ps << endl;
> // leave the function without delte, the auto_ptr will release it
> automatically
> }
>
> TIA.
> ABAI


 
Reply With Quote
 
 
 
 
benben
Guest
Posts: n/a
 
      10-24-2006
> I am reading a chinese book about STL, the book says below code will
> have no memory leak. I think its magic and wonder why the auto_ptr
> knows the memory should be freed when leaving the function call:
>
> void func()
> {
> auto_ptr<string> ps(new string("jjhou"));
> cout << *ps << endl;
> // leave the function without delte, the auto_ptr will release it
> automatically
> }


A google on auto_ptr should give you tons of information. Basically, the
auto_ptr class template is an example of how RAII idiom can help
resource management.

But before you get too excited please note that auto_ptr may not the be
all and end all solution--far from it, in fact. Google on it, see what
restrictions it has and what policy it imposes.

That said, auto_ptr is a handy tool to use in the face of exception
safety simply by the fact that it is properly destroyed (unlike a raw
pointer) during stack unwinding. Again, google on it.

Regards,
Ben
 
Reply With Quote
 
Ye Dafeng
Guest
Posts: n/a
 
      10-24-2006
Binary wrote:
> Hi,
>
> I am reading a chinese book about STL, the book says below code will
> have no memory leak. I think its magic and wonder why the auto_ptr
> knows the memory should be freed when leaving the function call:
>
> void func()
> {
> auto_ptr<string> ps(new string("jjhou"));
> cout << *ps << endl;
> // leave the function without delte, the auto_ptr will release it
> automatically
> }
>
> TIA.
> ABAI
>


I suggest you read << C++ Primer >>, maybe you can find what you want.
 
Reply With Quote
 
Clark S. Cox III
Guest
Posts: n/a
 
      10-24-2006
Binary wrote:
> Hi,
>
> I am reading a chinese book about STL, the book says below code will
> have no memory leak. I think its magic and wonder why the auto_ptr
> knows the memory should be freed when leaving the function call:


Because that is what auto_ptr's destructor does; it deletes the pointer
that it is holding on to.

--
Clark S. Cox III
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
explicit auto_ptr<T>::auto_ptr(T*) ? Sousuke C++ 9 03-16-2010 11:54 AM
magic / counter-magic? (detect loading file?) Giles Bowkett Ruby 9 12-17-2007 05:42 AM
auto_ptr<Derived> to auto_ptr<Base> Siemel Naran C++ 2 01-11-2005 04:45 AM
shared_ptr vs std::auto_ptr SerGioGio C++ 3 07-03-2003 02:52 PM



Advertisments