When to use smart pointers in real life?

I'm 13 but I program since I was 8. When I was that young, I was used to use pointers and dynamic arrays. But today, I saw everything I can do with a pointer I can do with a vector. I saw the smart pointers last week, I can't see a real use for scoped, shared, weak, auto, etc.
When would I use a pointer like that, instead of vectors? Why? And how? Can't it be replaced with any other algorithm (STD libraries), memory, container?
You will use smart pointers whenever the pointer points to a resource that must be released when the pointer goes out of scope (that is, when you're done with it).

But today, I saw everything I can do with a pointer I can do with a vector.
A pointer can point to pretty much anything, a vector is just a dynamic array.

Consider handing a shared library. You can use a smart pointer defined to release with dlclose() (or FreeLibrary), then initialise it with dlopen() (or LoadLibrary). When the pointer goes out of scope, it releases the library cleanly.
When to use smart pointers in real life?

In *most* cases, any time you would otherwise new up a raw pointer, for the reason kbw specifies.

When would I use a pointer like that, instead of vectors?

Not sure why you think pointers and containers seem to be related?
When you were 8, std::vector was already standardized I believe, I'm not sure why you haven't learned it.

When would I use a pointer like that, instead of vectors?
You don't. Smart pointers hold a single value.

I saw the smart pointers last week, I can't see a real use for scoped, shared, weak, auto, etc.
If you see auto pointers, just run away.

I use shared pointers for my game engine's resource manager, because objects need to have a "ownership" of my resources.
Last edited on
I know they are not related to containers. I wrote that because when I was eight I was use it to use pointers like that. I will look at C++ references to know how to use smart pointers.
Can anyone give me some examples?
@Avilius These days I didn't even know what's a class.
I will look at C++ references to know how to use smart pointers.

Eh?

Just google "std::unique_ptr" for starters.
it is mostly used when you want to represent the index of a number differently...
eg. a=40, *a=20......
it means a is pointed by 20...
alfredokang wrote:
it is mostly used when you want to represent the index of a number differently...

I don't know what this means or how it relates to smart pointers.

alfredokang wrote:

eg. a=40, *a=20
it means a is pointed by 20...

That snippet means the programmer has no idea how to use a.

The subject is "smart pointers" and when to use them. The subject is not "how raw pointers differ from ints." When you do supply examples to support your assertions, please use valid syntax and complete code in the snippet.
kk..i hear...
it is mostly used when you want to represent the index of a number differently...
not really. you can errr iterate differently i guess, but you would have to still index the same, but i dont know why you would do that at all with pointers
I agree with the ownership concept. Suppose you want to make a new pointer stored in one variable, but then you pass it around to other variables. At some points you might have many variables storing the same pointer. If you have variety in which one ends up having the pointer in memory last, smart pointers can, in principle, help you avoid deallocation when you don't want to do that and gain a final deallocation in the right place and time. For example, think about when a destructor is called. Some smart pointers will not deallocate the memory in a destructor if it is not the last variable using the same pointer. Keep in mind that you can design your own smart pointers--you aren't limited to standard ones that are available, and it's a general concept that you can implement inside your classes.

I personally run into some problems with standard smart pointers. They are not pointers, and in some syntax situations where you want a pointer, C++ won't automatically do the same type conversions, for example, when inheritance is involved. My own two cents at this stage is that it's better to come up with a game plan for memory management and use real pointers than it is to use a smart pointer and ignore software design strategies. Does that make any sense?
Last edited on
They are not pointers, and in some syntax situations where you want a pointer, C++ won't automatically do the same type conversions


This is intentional, as it is "dangerous" in that it allows you to break ownership. However if you need a raw pointer from a smart pointer, there's a 'get' member function which gives you the raw pointer.

when inheritance is involved.


Smart pointers implicitly cast to smart pointers of parent classes just as raw pointers do:

1
2
3
4
5
6
7
8
9
class Parent { };
class Child : public Parent { };

int main()
{
    std::shared_ptr<Child>  c( new Child );
    std::shared_ptr<Parent> p;

    p = c;  // legal.  Compiles just fine.  Does what you'd expect. 


You can also downcast to a child class with static_pointer_cast and dynamic_pointer_cast:

 
    c = dynamic_pointer_cast<Child>(p);  // legal, does what you'd expect. 


My own two cents at this stage is that it's better to come up with a game plan for memory management and use real pointers than it is to use a smart pointer and ignore software design strategies. Does that make any sense?


Not really. The two are not mutually exclusive.

You can (and should) use smart pointers and have a good design plan. One does not prevent the other.

In addition to preventing memory leaks, smart pointers also help a great deal with exception safety. They (along with STL containers) also tend to make encapsulation easier, since you do not usually have to write custom copy ctors/assignment operators/destructors for classes like you have to where you have manual memory management.

Really, there is little/no reason to ever not use a smart pointer or STL container. My general rule is... if you are manually deleting or manually freeing resources - you're doing it wrong.
Last edited on
closed account (j3Rz8vqX)
I don't want to combat any ideas here, but I relate to it as:

Higher level coding vs lower level coding.

[...]

Somewhat like:

Vectors vs primitive arrays.. wait that isn't fair.

Alright, how about:

Strings vs c_strings.. that's about right; bandwagon to strings for increased productivity.

What I'm saying is:

I don't want to discredit pointers, but like everyone's stated, there are better means towards producing code that needs pointing.

---
Assembly, pushing and popping of registers are fun to do, if you know what you're doing, but they simply aren't as easily maintainable and definitely less time efficient when producing.
Last edited on
Compilers have advanced to the point that your bad C++ code can generally run faster than your good assembly code. Compilers know how to optimize better than you ever could.

Using raw pointers disables a handful of potential optimizations. Don't do it.
closed account (j3Rz8vqX)
@LB, Nothing was mentioned about speed. I was illustrating that lower level language are less productive than higher level languages.
By the way:
bad C++ code can generally run faster than your good
C++ code; speed is based on the algorithm, not the language.

When would I use a pointer like that, instead of vectors?

pointer != vector;
Use it to point to things.
You will most definitely use containers more often than pointers because pointers are aimed for aliasing.

smart pointers

smart pointer == Optimization encapsulating pointer;
Can be the same as pointers with additive safety and features; similar to vector versus array.

Rule of thumb is don't reinvent the wheel and use what your best comfortable with.
Dput wrote:
speed is based on the algorithm, not the language.
What did you think I meant by "good" and "bad"?

And actually, the design of a language greatly influences the availability of compiler optimizations.
Last edited on
Topic archived. No new replies allowed.