auto_ptr and sink

A

Alf P. Steinbach

auto_ptr has the attribute release(). then what is the need of sink function ??

could you translate that question to English, please


cheers,

- Alf
 
J

Jorgen Grahn

could you translate that question to English, please

I don't think the problem is the English -- it's that sink function
which I've never heard of. Asit, what and where is the sink function?

/Jorgen
 
S

SG

Please read this

http://www.gotw.ca/publications/using_auto_ptr_effectively.htm

please read "Ownership, Sources, and Sinks" section

So?
I still don't understand the question. source() and sink() are just
examples and not part of auto_ptr's interface whereas your question
seems to suggest you think sink is part of auto_ptr's interface.

Do you understand the concept of an "example"?

Please put a little more effort into writing questions. I'm fairly
sure that most of the comp.lang.c++ members lack mind reading
capabilities.

Cheers!
SG
 
A

asit

I asked this because I found certain code snippets(in application, not in example) which mostly uses Source-Sink, not release method of the auto_ptr class.

Is there any advantage of using sink() instead of release() of auto_ptr ???
 
A

Asger-P

Hi asit

I asked this because I found certain code snippets(in application, not
in example) which mostly uses Source-Sink, not release method of the
auto_ptr class.

Is there any advantage of using sink() instead of release() of auto_ptr
???

If You want to do some work to the object, that auto_ptr is pointing to,
before it gets deleted there can be, but if not sink is just a fancy
way of of calling auto_ptr::reset();

BUT.. to me it sounds like You are mixing reset and release

here is an example of the use of release:

std::auto_ptr<TBitmap> Bmp( new TBitmap() );

// do stuf that can throw

BmpVector.push_back( Bmp.get() );

Bmp.release();


Now the bitmap pointer will not get deleted when auto_ptr runs
out of scoope, because You have released auto_ptr of its ownership
of the bitmap.

If You on the other hand want to "release" the memory of the object
that is pointed to by auto_ptr You have to use auto_ptr::reset instead.

std::auto_ptr<TBitmap> Bmp( new TBitmap() );
// do stuf that can throw
Bmp.reset(); // free the memory

auto_ptr::reset do a delete on the internal object and set its
internal pointer to NULL


Best regards
Asger-P
 
A

Asger Joergensen

Hi asit
I asked this because I found certain code snippets(in application, not in example) which mostly uses Source-Sink, not release method of the auto_ptr class.

Is there any advantage of using sink() instead of release() of auto_ptr ???

If You want to do some work to the object, that auto_ptr is pointing to,
before it gets deleted there can be, but if not sink is just a fancy
way of of calling auto_ptr::reset();

BUT.. to me it sounds like You are mixing reset and release

here is an example of the use of release:

std::auto_ptr<TBitmap> Bmp( new TBitmap() );

// do stuf that can throw

BmpVector.push_back( Bmp.get() );

Bmp.release();


Now the bitmap pointer will not get deleted when auto_ptr runs
out of scoope, because You have released auto_ptr of its ownership
of the bitmap.

If You on the other hand want to "release" the memory of the object
that is pointed to by auto_ptr You have to use auto_ptr::reset instead.

std::auto_ptr<TBitmap> Bmp( new TBitmap() );
// do stuf that can throw
Bmp.reset(); // free the memory

auto_ptr::reset do a delete on the internal object and set its
internal pointer to NULL


Best regards
Asger-P
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top