0

Is auto_ptr good to work with only for local variables?
If I need to work with classes too, do i need copied pointer ?

6
  • 2
    I really don't know what's being asked here.... Commented Mar 1, 2011 at 23:03
  • What would be the purpose of auto_ptr for local variables? Why would you need any different behavior for classes? Your question leaves more questions. Commented Mar 1, 2011 at 23:05
  • well,if i use auto_ptr for a function variable,when i exit the function doesnt the destructor being called ? Commented Mar 1, 2011 at 23:08
  • @vBx, yes, that's the entire purpose of smart pointers in general (which is part of what makes your question confusing). Note, however, that in recent compilers, std::auto_ptr has been deprecated in favor of std::unique_ptr. Commented Mar 1, 2011 at 23:18
  • 2
    From your comment, maybe you need to review the concept of scopes. I believe if you understood scoping better you would have the answer to your question. Commented Mar 1, 2011 at 23:19

2 Answers 2

3

The auto_ptr destructor does deallocate the memory, so you are correct. Once you leave the auto_ptr's scope the thing you're pointing to will go away. You might be looking for something like a shared_ptr, which is a reference counted smart pointer. It will be part of the next C++ standard. Boost has an implementation of it, and it's also part of the TR1 library. If your compiler supports std::tr1 entities then you should have the shared_ptr.

Edit

As pointed out in comments, auto_ptr copy semantics cause a transfer of ownership that does not necessarily result in deletion of the object. So, an auto_ptr type variable can be assigned to another, and could be used as a function return value. The key with auto_ptr is that only one of them at a time can reference a particular entity.

I think I was assigning the traits of scoped_ptr to auto_ptr incorrectly and a little unfairly. My own bias is against auto_ptr because that transfer of ownership causes a side effect on the source object that is not normally associated with copying.

Sign up to request clarification or add additional context in comments.

3 Comments

Note that boost::scoped_ptr or std::unique_ptr are the apples-to-apples proper replacements for std::auto_ptr.
actually std::auto_ptr's copy constructor implements ownership transfer semantic, so if you return a local auto_ptr the ownership will be transferred to the returned instance of auto_ptr i.e. to the caller.
@matteo: I stand corrected. Thank you. Will edit to reflect that knowledge.
0

You aren't required to use pointers in C++ in many cases, so you may not need any kind of smart pointer:

struct Foo {
    int bar;
    int twice_bar()
    {
        return 2 * bar;
    }
};

int twice_x(int x)
{
    Foo f;
    f.bar = x;
    return f.twice_bar();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.