10

What is the objective of scoped pointer? to my understanding, the scoped pointer manages the memory within a block of code. If i want to declare a variable within a block , i can just declare it on a stack and not worry about cleaning.

MPelletier
  • 15,130
  • 14
  • 79
  • 128
Jimm
  • 7,173
  • 14
  • 59
  • 106

4 Answers4

5

Not if it's of dynamic size or type. In addition, scoped pointers can be swapped, and in C++11 unique_ptr can be moved, so they're not strictly scoped.

Puppy
  • 138,897
  • 33
  • 232
  • 446
  • 1
    Except that "The primary reason to use `scoped_ptr` rather than `auto_ptr` is to let readers of your code know that you intend "resource acquisition is initialization" to be applied only for the current scope, and have no intent to transfer ownership"... seems like `swap` is not an intended usage. – Ben Voigt May 24 '12 at 00:08
  • @BenVoigt: But it is provided and can be used. – Puppy May 24 '12 at 00:09
  • 2
    Here's [an old thread](http://lists.boost.org/Archives/boost/2002/09/36359.php) discussing why `swap` became a requirement (basically so `scoped_ptr` can be used as a member of a copyable class). – Jesse Good May 24 '12 at 00:26
  • What do you mean by dynamic size or type? – Jimm May 25 '12 at 02:01
5

Unlike stack-based data, scoped_ptr has a reset() member -- in other words, you can construct/destruct to your heart's content. With this, you can use a null pointer (technically operator unspecified-bool-type) as a flag indicating whether or not there is a constructed object at any given time. It also allows you to sequence construction/destruction independently from the variable scope if that is needed.

Also, consider that you can declare a scoped_ptr as a class member, not just as a stack variable. The docs suggest using scoped_ptr to implement the handle/body idiom (to hide the class' implementation details).

Finally, to elaborate on DeadMG's point "Not if it's of dynamic type", you can use scoped_ptr to implement a polymorphic operation:

{
scoped_ptr<Base> a( mode ? new DerivedA : new DerivedB );
a->polymorphic_function();
}

It's not really possible to do this with simple stack-based allocation.


Also see here: C++0x unique_ptr replaces scoped_ptr taking ownership?

Community
  • 1
  • 1
Brent Bradburn
  • 40,766
  • 12
  • 126
  • 136
2

The point is that you can create and clean up a pointer within a certain lexical scope. This can be useful in a variety of situations, and it assures you have no memory leaks, by forgetting a delete if you were to use new explicitly, which is not recommended.

You should keep in mind that the boost::scoped_ptr is non-copyable, and so owns it's resource entirely, for the entire duration of it's lifetime. This also makes it safer then boost::shared_ptr, as it avoids copying the resource or accidentally sharing it.

{ //Some Scope

  boost::scoped_ptr<int> i_ptr;
  // do something with pointer

} // leave scope, pointer is cleaned up
Tony The Lion
  • 57,181
  • 57
  • 223
  • 390
1

usually thread stacks have memory limits (see thread stacksize).

also sometimes the pointer might have been passed to you from outside and needs to be deleted in this scope (e.g. if an exception is thrown, any delete call below that line won't get executed). So you need someway of auto-magically cleaning up the pointer

void foo(Object*obj)
{
    //this will ensure that object gets cleaned up even if doFoo() throws an exception
    boost::scoped_ptr<Object> objCleaner(obj);
    obj->doFoo();
}
mohaps
  • 980
  • 4
  • 10
  • I can declare the pointer on stack and it would be automatically cleaned when the function goes out of scope, either successfully or via exception. Correct? – Jimm May 25 '12 at 01:59
  • { X* x = &someObj; } this will not cause the pointer x to be deleted when it goes out of scope. someObj will be deleted when it goes out of scope. {X* y = new X();} this type of code will require scoped ptr for auto clean up – mohaps May 30 '12 at 05:36