Modern C++ Use shared_ptr instead of raw point #1123
Zyres
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
SmartPointers come in handy if you want to invest your time in the code instead of memory management.
As an example we have a lot Structures and Classes created by "new". There is a lot going on in the background by using this. You allocate space on the heap so we can point to it (*) from anywhere in our programm.
Long story short... if you use "new" you have to use "delete" at the end of your programm.
There are a lot of these "delete" in our source too. Sure we could say "Memory allocation is too important to leave it to the programm" but in most cases it is just a waste of time dealing with it. (tbh. if an object gets destructed just let the memory go...).
Here is an example illustrating raw pointers:
Here is that example with shared_ptr:
Beta Was this translation helpful? Give feedback.
All reactions