Smart pointers, shared.

Well I am studying Smart Pointers and don't understand when a shared pointer points to the same object. That is, why is it pointing to the same object more than once?

Thank you,

Josheir
Can you give an example about what you mean?
From what I've read shared pointers share what there pointing too. They are all pointing to the same object? I don't understand when this would be used. When all the pointers are no longer pointing to an object the pointers are destroyed.

Or perhaps they arn't pointng to the same object at all?

Thank you,

Josheir

Why would you want more than one pointer pointing to the same object?

I have no example, just what I've read on shared_ptr.
Smart pointers are objects that wrap "raw" pointer functionality, and provide an interface for them. You're left not having to worry about things like delete-ing pointers by using them.

A shared pointer or std::shared_ptr is a certain kind of smart pointer that implements a specific policy as to the lifetime of whatever it's pointing to. Shared pointers have an internal reference counter, which is useful if the lifetime of the object that's pointed to isn't limited to one scope, or necessarily known beforehand. By keeping track of how many shared pointers are pointing to the same resource, you're guaranteed that when the last shared pointer dies, it will destroy what it's pointing to as well.
Last edited on
They are all pointing to the same object?

Not "all". You can have pointers to one object and other pointers to different object.
I don't understand when this would be used.
1
2
3
4
5
6
7
8
void foo( int * bar ) {
}

int main( int argc, char ** argv ) {
  int XX = 42;
  int * gaz = &XX;
  foo( gaz );
}

These are not smart pointers, nor point to dynamically allocated memory, but they show a "two to same" situation. The bar is a copy of gaz. Thus they both point to same object: XX. Making copies is very common. The argv points to different object(s) than gaz and bar.

When all the pointers are no longer pointing to an object the pointers are destroyed.

Almost, but not quite (and the devil of programming is in the details).

When the last shared_ptr that points to specific dynamically allocated object Z ceases to point to Z, the Z is destroyed.
The most common case, where shared_ptr ceases to point, is destruction of the pointer; the destructor of the pointer can delete the pointed to object.
That is not the only case. For example, the shared_ptr has member reset(). That can delete the pointed to object, but the pointer is a separate object that continues to exist.

In the above example the pointer bar exists only for the duration of the function call. The pointers gaz and argv live longer; to the end of main().

If the gaz and bar were std::unique_ptr's, the ownership would transfer form gaz to bar and gaz would no longer "own" pointed to object when foo() starts. The bar would delete the object at the end of foo().

If they were shared_ptr's, they would co-own (share) object for the duration of foo(), and gaz would do the deletion at the end of main().
Yes, I do understand. That's what I was wondering Keskiverto. I thought maybe the shared pointers were all global or in the same scope. Now I think I understand.

Josheir
Topic archived. No new replies allowed.