auto_ptr

Folder *imf = (*auto_mbox)->get_select_folder();

how do i use a auto_ptr to represent imf?

i have tried

auto_ptr<Folder> imf (new IMAP_Folder((*auto_mbox)->get_select_folder()));

but that does not work

VlearnsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

evilrixSenior Software Engineer (Avast)Commented:
>> but that does not work

define "does not work".

does get_select_folder() return a heap allocated pointer created using the new operator? If not auto_ptr cannot be used!

In your second example, assuming it does, you're trying to use a pointer to initialise the constructor of a IMAP_Folder object. Is this correct?

Did you try this?

std::auto_ptr imf ( (*auto_mbox)->get_select_folder() );
0
VlearnsAuthor Commented:
heres tehd eclaration of auto_mbox

auto_ptr<MailboxAutoOpener> auto_mbox(new MailboxAutoOpener(_session.mb
ox()));
0
evilrixSenior Software Engineer (Avast)Commented:
That doesn't really help much :)

Please see my previous post where I show how to use auto_ptr based upon my understanding of the type returned but also the importance of making sure the pointer returned is suitable to be managed by an auto_ptr.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

VlearnsAuthor Commented:
Folder* get_select_folder(bool update = false)
heres what you need, is it ok to use auto_ptr?
0
evilrixSenior Software Engineer (Avast)Commented:
>> heres what you need, is it ok to use auto_ptr?
Again, I can't answer that since all that tells me is it returns a pointer but that doesn't tell me if the memory the pointer references was created using new.

What I can tell you is that syntactically the example I show you above will work. What I can't tell you is if this will be safe. You need to check to see how the memory the pointer references is allocated. If this is a 3rd partly library you'll need to refer to the documentation.
0
VlearnsAuthor Commented:
thanks gazillion, i will check it out

bool  Command::fne(
    const char* tag,
    const char* name,
     Folder *imf
if i wanted to sent the auto pointer to this function  (assuming its correct) what would be the equibvalent signature

sorry i am new to auto_ptrs...
0
evilrixSenior Software Engineer (Avast)Commented:
The canonical form for using auto_ptr is always this...


auto_ptr< TYPE > varname

So, if you wish to define an auto_ptr of type Folder the form will be this

auto_ptr< Folder> varname

To get from a pointer to an auto_ptr just lose the * and move the type into the angled brackets.

Folder *

Lose the *

Folder

Put type in angled brackets

< Folder >

Prefix with auto_ptr

auto_ptr< Folder >

Does that make sense?
0
VlearnsAuthor Commented:
thanks that helps

i have changed the signatures for the function,

bool  Command::fne(
    const char* tag,
    const char* name,
     auto_ptr<Folder>


before changing the function, imf was passed as follows:


bool  Command::fne(
    tag,
    name,
     imf)

now that i have chanhed teh interfaces, the compiler complains that i am passing
 auto_ptr<Folder> in my call when the signature is

auto_ptr<Folder> &





0
evilrixSenior Software Engineer (Avast)Commented:
Can you show me the code please?
0
VlearnsAuthor Commented:
actually let me try this out, i could be wrong
0
VlearnsAuthor Commented:
although i do understand how instances of *imf are to be replaced

i have places where  imf->uidnext()

how would use of imf like above (without the *) change after using auto_ptrs
0
evilrixSenior Software Engineer (Avast)Commented:
It doesn't. The whole point is that from an interface point of view auto_ptr is the same as a normal pointer. It respects all the normal pointer dereference and indirection operators that you would normally use on a pointer.

Folder * pf1 = new Folder;
auto_ptr pf2(new Folder);

pf1->uidnext() ;
pf2->uidnext() ;

Folder const & r1 = *pf1;
Folder const & r2 = *pf2;
0
VlearnsAuthor Commented:
what is the imapct if teh auto_ptr above is created for a memory created using malloc and not new?...what re the safety issue?
0
evilrixSenior Software Engineer (Avast)Commented:
well auto_ptr calls delete, when is the C++ operator to release memory back to the free-store from a call to the C++ new operator. On the other hand, malloc is a C function that allocates dynamic memory from the heap and free releases it back to the heap. It may be that free-store and heap are actually the same thing but it is equally possible that they are not (the C++ standard leaves this undefined, it is up to the compiler), in which case you'll be releasing memory back to the wrong place. So, mixing up new/delete with malloc/free is prohibited, with the result being undefined (usually, this means something bad will happen).

Herb Sutter does a better job of describing the Free-store and Heap at the following link.
http://www.gotw.ca/gotw/009.htm
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
VlearnsAuthor Commented:
thanks a million!!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.