SMART POINTERS in C++ (std::unique_ptr, std::shared_ptr, std::weak_ptr)

The Cherno
11 Sept 201711:37

Summary

TLDRThis video script delves into the concept of smart pointers in C++, a feature designed to automate memory management. It explains that smart pointers, such as unique and shared pointers, act as wrappers around raw pointers to handle memory allocation and deallocation automatically. Unique pointers prevent copying to avoid multiple owners of the same memory, ensuring automatic deletion when out of scope. Shared pointers, on the other hand, use reference counting to manage multiple references safely. The script also touches on weak pointers for non-owning references. The presenter suggests that while smart pointers are beneficial for preventing memory leaks, they don't entirely replace the need for manual memory management with 'new' and 'delete', recommending the use of unique pointers by default due to their lower overhead.

Takeaways

  • 📌 Smart pointers in C++ are designed to automate memory management, eliminating the need for manual calls to `new` and `delete`.
  • 🔑 A smart pointer acts as a wrapper around a raw pointer, managing the allocation and deallocation of memory on the heap.
  • 👤 Unique pointers (`std::unique_ptr`) are non-copyable and are used for exclusive ownership of a resource, automatically deleting the object when the pointer goes out of scope.
  • 🔄 The copy constructor and assignment operator for unique pointers are deleted to prevent multiple owners of the same resource, which could lead to double deletion and undefined behavior.
  • 🚫 `std::make_unique` is the preferred way to create unique pointers due to its exception safety; it avoids potential memory leaks if an exception occurs during object construction.
  • 🤝 Shared pointers (`std::shared_ptr`) allow for shared ownership of a resource through reference counting, where the resource is only deleted when the last shared pointer goes out of scope.
  • 📈 Reference counting in shared pointers involves allocating an additional control block to keep track of the number of references, which adds some overhead compared to unique pointers.
  • 🔄 `std::make_shared` is the recommended method for creating shared pointers as it is more efficient and exception-safe compared to using `new` followed by the shared pointer constructor.
  • 🔗 Weak pointers (`std::weak_ptr`) provide a non-owning reference to an object managed by shared pointers, useful for breaking reference cycles and can be used to check if the object is still valid.
  • ♻️ Smart pointers help prevent memory leaks and simplify memory management, but they are not a complete replacement for `new` and `delete`, especially in cases where explicit control over memory allocation is necessary.
  • 🛠️ The choice between unique and shared pointers depends on the use case, with unique pointers being the first preference due to lower overhead, followed by shared pointers when sharing is necessary.

Q & A

  • What are smart pointers in C++?

    -Smart pointers in C++ are a feature that automates memory management. They wrap around a raw pointer, managing the allocation and deallocation of memory on the heap, so you don't have to manually call 'new' and 'delete'.

  • Why should one consider using smart pointers?

    -Smart pointers should be considered for their ability to automate memory management, preventing memory leaks by ensuring that memory is properly freed when it's no longer needed, without the programmer having to explicitly call 'delete'.

  • What is the difference between a unique pointer and a shared pointer in C++?

    -A unique pointer is a smart pointer that owns the object it points to and ensures that there is only one owner of the object. It cannot be copied. A shared pointer, on the other hand, maintains a reference count to keep track of how many pointers share ownership of the same object, and it can be copied.

  • Why are unique pointers called 'unique'?

    -Unique pointers are called 'unique' because they must be the sole owner of the object they point to. Copying a unique pointer is not allowed to prevent multiple owners from causing issues when the memory is freed.

  • What is the preferred way to create a unique pointer in C++?

    -The preferred way to create a unique pointer is by using 'std::make_unique', which is safer in terms of exception handling because it prevents the creation of a dangling pointer in case of an exception during object construction.

  • How does the reference counting mechanism work with shared pointers?

    -Reference counting with shared pointers works by keeping track of how many shared pointers are pointing to a particular object. When the reference count reaches zero, indicating no shared pointers are referencing the object, the object is deleted and its memory is freed.

  • What is a weak pointer and what is its purpose?

    -A weak pointer is a smart pointer that does not increase the reference count when it holds a reference to an object. It is used to break reference cycles and to hold a non-owning reference to an object that is managed by shared pointers.

  • What is the overhead associated with using shared pointers?

    -The overhead associated with shared pointers comes from the additional memory required for the reference count and the control block, as well as the potential performance cost of incrementing and decrementing the reference count.

  • Why should one avoid using 'new' with shared pointers?

    -Using 'new' with shared pointers is not recommended because it requires an additional allocation for the control block, which can be avoided by using 'std::make_shared', making the process more efficient and exception-safe.

  • What is the general recommendation for when to use unique pointers versus shared pointers?

    -Unique pointers should be used as the first preference when you need a single owner for the object. Shared pointers should be used when you need to share ownership of an object between multiple pointers, but be aware of the additional overhead they introduce.

Outlines

00:00

🔒 Introduction to Smart Pointers in C++

The video script introduces the concept of smart pointers in C++, explaining their purpose and functionality. The narrator discusses how smart pointers automate memory management by eliminating the need for explicit calls to 'new' and 'delete'. The script introduces 'unique pointers' as a type of smart pointer that cannot be copied, ensuring that there is only one owner of the memory block it points to. This is crucial for preventing memory leaks and ensuring safe programming practices. The video also touches on the idea of stack allocation and object lifetimes, suggesting that viewers refer to a previous video for more details on these topics.

05:01

🔄 Understanding Unique and Shared Pointers

This paragraph delves deeper into the workings of unique pointers, emphasizing their role in stack allocation and their inability to be copied due to the risk of double deletion. The script then contrasts unique pointers with shared pointers, which utilize reference counting to manage memory. Shared pointers allow multiple pointers to reference the same memory block, safely incrementing and decrementing a reference count to determine when the memory can be freed. The video script provides a practical example of using shared pointers, demonstrating how they can be copied and how the reference count affects their behavior. Additionally, the script introduces weak pointers as a way to hold a non-owning reference to an object pointed to by a shared pointer.

10:02

🛠️ Practical Usage and Recommendations for Smart Pointers

The final paragraph discusses the practical usage of smart pointers in C++ programming. The narrator suggests that smart pointers should be used whenever possible due to their ability to automate memory management and prevent memory leaks. The script provides guidance on when to use unique pointers versus shared pointers, recommending unique pointers for their lower overhead and shared pointers for cases where memory needs to be shared. The narrator also acknowledges that while smart pointers are powerful, they have not completely replaced the need for 'new' and 'delete' in certain scenarios. The video concludes with a call to action for viewers to engage with the content through likes, comments, and support on Patreon.

Mindmap

Keywords

💡Smart Pointers

Smart pointers in C++ are a feature that automates memory management by wrapping a raw pointer with additional functionality. They are designed to handle the allocation and deallocation of memory on the heap, eliminating the need for explicit calls to 'new' and 'delete'. In the video, smart pointers are introduced as a way to make programming safer by preventing memory leaks and ensuring that resources are properly managed. The script discusses different types of smart pointers, such as unique pointers and shared pointers, and how they can be used to manage object lifetimes.

💡Unique Pointers

Unique pointers are a type of smart pointer that ensures a single ownership of the underlying resource. They are unique in the sense that they cannot be copied, which prevents multiple pointers from owning the same resource and potentially causing memory leaks when one of them is destroyed. The video script provides an example of creating a unique pointer for an 'Entity' class and emphasizes the use of 'std::make_unique' for exception safety. The unique pointer is destroyed when it goes out of scope, automatically deallocating the memory.

💡Stack Allocation

Stack allocation refers to the process of allocating memory for variables on the call stack, which is a region of memory that stores information about the active subroutines or functions in a program. In the context of the video, stack allocation is mentioned as a way to leverage the lifetime of objects without the need for manual memory management. Unique pointers, being stack-allocated objects, will automatically call 'delete' on the underlying pointer when they are destroyed, thus freeing the memory.

💡Reference Counting

Reference counting is a memory management technique used by shared pointers in C++. It involves keeping track of the number of references to an object and deallocating the object when the count reaches zero. This mechanism allows multiple pointers to share ownership of the same resource, ensuring that the resource is not freed until all references to it are gone. The video explains that shared pointers use reference counting to manage the lifetime of the objects they point to, with the reference count being stored in a control block.

💡Shared Pointers

Shared pointers are another type of smart pointer that allow multiple pointers to share ownership of a dynamically allocated object. They use reference counting to manage the object's lifetime, which means the object is only destroyed when the last shared pointer that points to it is destroyed or reassigned. The script demonstrates how shared pointers can be copied without causing memory leaks, as each copy increments the reference count.

💡Memory Management

Memory management is the process of allocating, deallocating, and managing the use of memory in a computer program. In the context of the video, memory management is a central theme, as smart pointers are introduced as a means to automate and simplify this process. The video discusses how smart pointers prevent memory leaks by automatically freeing memory when objects are no longer needed, and how they can be used to avoid the explicit use of 'new' and 'delete'.

💡Exception Safety

Exception safety refers to the ability of a program to handle exceptions without causing memory leaks or other resource management issues. In the video, the use of 'std::make_unique' for constructing unique pointers is highlighted as a safer alternative to directly using 'new', as it prevents the creation of a dangling pointer in case of an exception during object construction.

💡Control Block

A control block is a separate block of memory allocated by a shared pointer to store the reference count and other management data. This block is necessary for shared pointers to keep track of how many shared pointers are currently referencing the same object. The video mentions that creating a shared pointer with 'new' requires an additional allocation for the control block, which is why 'std::make_shared' is preferred for efficiency.

💡Weak Pointers

Weak pointers are a type of smart pointer that do not own the underlying resource but instead observe the reference count of a shared pointer. They are used to break reference cycles and to access an object without increasing its reference count. The video explains that weak pointers can be used to check if the observed object is still alive without affecting its lifetime.

💡Ownership

Ownership in the context of smart pointers refers to the concept of which pointer is responsible for the management and eventual deallocation of the memory of the object it points to. Unique pointers have sole ownership, meaning no other pointer can point to the same object. Shared pointers, on the other hand, allow multiple pointers to share ownership based on the reference count. The video emphasizes the importance of understanding ownership when choosing between unique and shared pointers.

Highlights

Introduction to smart pointers in C++ and their automation of memory management.

Explanation of how smart pointers eliminate the need for manual calls to 'new' and 'delete'.

Overview of unique pointers and their single ownership semantics.

Unique pointers' inability to be copied to prevent double deletion of the same memory block.

Demonstration of creating a unique pointer and its automatic destruction when out of scope.

Introduction to 'std::make_unique' for safer exception handling when creating unique pointers.

Difference between unique pointers and shared pointers regarding object lifetime management.

Description of shared pointers and their use of reference counting for memory management.

Shared pointers' ability to be copied and their impact on reference count.

Advantages of using 'std::make_shared' for efficiency and exception safety.

Explanation of weak pointers and their role in not increasing the reference count.

Use cases for weak pointers when you do not want to take ownership of the object.

Discussion on when to use smart pointers and the preference for 'unique_ptr' over 'shared_ptr'.

The presenter's perspective on smart pointers not completely replacing 'new' and 'delete'.

Recommendation to use unique pointers for lower overhead when appropriate.

Encouragement to use smart pointers to automate memory management and prevent leaks.

Invitation for viewers to join the presenter's Discord server for further questions and discussions.

Call to action for supporting the series on Patreon for additional content and rewards.

Transcripts

play00:00

hey what's up guys my name is a channel

play00:01

welcome back to my state boss last

play00:02

series today we're gonna be talking all

play00:04

about smart pointers in C++

play00:06

so as my pointers are a topic that has

play00:07

come up a lot recently in my videos and

play00:10

people requesting this and what is a

play00:12

smart pointer should I be using smart

play00:13

pointers and all that today we just goes

play00:15

over what they are we're not really

play00:17

going to get into depth about what why I

play00:18

think you should or shouldn't be using

play00:20

them and all that stuff and my opinions

play00:22

about smart pointers in general I'm

play00:24

gonna save that for another video today

play00:25

we're just gonna focus on what a smart

play00:27

pointer is and what it can do for you so

play00:29

earlier we talked about what new and

play00:30

delete does new allocates memory on the

play00:31

heap and delete is needed to delete that

play00:33

memory to free that memory because it

play00:35

won't be freed automatically smart

play00:36

pointers are a way to automate that

play00:39

process that's all they are right smart

play00:41

pointers mean that when you call new you

play00:44

don't have to call delete and in fact in

play00:46

many cases with smart pointers we don't

play00:48

even have to call new so a lot of people

play00:50

tend to have this kind of programming

play00:52

style and safe as possible they never

play00:54

ever call new or delete and smart

play00:56

pointers are best their way to make that

play00:59

happen so smart pointers are essentially

play01:00

a wrapper around a real raw pointer when

play01:03

you create a smart pointer and you make

play01:05

it it will call new and allocate your

play01:07

memory for you and then based on which

play01:09

my pointer you use that memory will at

play01:12

some point be automatically free so

play01:13

let's take a look at the first kind of

play01:15

an simplest smart border that we have

play01:16

unique pointers so a unique pointer is a

play01:18

script pointer meaning that when that

play01:20

pointer goes out of scope it will it

play01:23

will get destroyed and it will call

play01:25

delete we actually talked about how

play01:26

object lifetimes work and how you can

play01:28

leverage the power of stack allocation

play01:30

all that in the last video so definitely

play01:31

check that out if you haven't already

play01:32

but that is basically our script points

play01:34

work or unique pointers there isn't

play01:36

they're called unique pointers it

play01:37

because they have to be unique you can't

play01:39

copy a unique pointer because if you

play01:41

copy a unique pointer the memory that

play01:44

it's pointing to they'll basically

play01:45

you'll have two pointers two unique

play01:47

pointers pointing to the same block of

play01:48

memory and when one of them dies it will

play01:50

free that memory meaning that suddenly

play01:52

that second unique pointer you had

play01:53

pointed to the same book of memory is

play01:55

pointing to memory that's been freed so

play01:57

you cannot copy unique pointers you need

play01:59

pointers are for when you want a skrub

play02:00

pointer and that is the only reference

play02:03

to that pointer you actually want let's

play02:05

take a look at an example

play02:05

a unique pointer so the first thing

play02:07

you'll need to do to get access to all

play02:08

these smart pointers is include memory

play02:10

now we have this entity class here or

play02:12

what is is the constructor on a

play02:14

destructor with print when we create the

play02:16

entity R when we destroy the entity just

play02:17

so that we can kind of look into the

play02:18

behavior of these smart pointers so over

play02:20

here in main if I want to create a

play02:22

unique pointer which lasts in a certain

play02:24

scope so I've made a new scope you an

play02:25

empty scope inside here I'm going to

play02:27

allocate my entity using a unique

play02:29

pointer the way I'll do that is I'll

play02:31

type in SV unique pointer I'll give it a

play02:34

template argument of entity and then

play02:37

I'll give it a name such as entity and

play02:38

then I've kind of got two options I

play02:40

could either call the constructor here

play02:41

and type in new entity like this note

play02:44

that you actually won't be able to do

play02:45

this kind of construction because if you

play02:49

look at unique pointer the constructor

play02:51

is actually explicit meaning that you do

play02:53

have to call the constructor explicitly

play02:54

there's no implicit kind of conversion

play02:56

or converting constructor so that's one

play02:58

way to make unique point and then you

play02:59

can kind of access it like you would

play03:01

anything else if I wanted to call a

play03:03

function here we don't even have any

play03:04

functions but if I were to call a

play03:06

function here I would just access it

play03:08

through the arrow operator and

play03:09

everything would be exactly the same as

play03:11

if this was just a roll pointer the

play03:12

preferred way though to construct this

play03:14

would actually be to assign it to STD

play03:16

make unique with that entity there the

play03:18

primary reason that that's important for

play03:20

unique pointers is actually due to

play03:22

exception safety we'll talk about

play03:23

exceptions at some point in this series

play03:25

I don't like exceptions at all so that's

play03:28

gonna be an interesting episode whenever

play03:29

that happens but anyway the preferred

play03:31

way to make this is to call make unique

play03:33

because it is slightly safer if if the

play03:36

constructor happens to throw an

play03:37

exception you weren't end up having a

play03:39

dangling pointer with no reference and

play03:41

that's a memory leak anyway the idea is

play03:43

that once we make this unique pointer we

play03:45

can call whatever method we want and

play03:46

you'll see that if I hit f5 to run my

play03:48

program our entity gets created here and

play03:51

then if I hit f10 to get out of this

play03:52

scope our entity is destroyed now

play03:54

okay so automatically when this code

play03:56

ends our entity gets destroyed that's

play03:58

the simplest my pointer that we have

play03:59

it's very useful it's got a very low

play04:01

overhead it doesn't really even have an

play04:03

overhead it's just a stack allocated

play04:05

object and when when that stack

play04:07

allocated object eyes it will call

play04:08

delete on your pointer and free that

play04:10

memory the problem with this is as I

play04:12

meant

play04:12

if you want to copy that at that point

play04:14

if you want to kind of share that point

play04:15

and maybe pass it into a function or

play04:17

have another class story you're gonna

play04:20

run into a problem because you can't

play04:22

copy it and if you take a look at this

play04:23

if I was to try and make another unique

play04:26

pointer here called easy row or

play04:29

something like that and assign it to

play04:30

entity I actually can't do that and

play04:32

you'll get kind of an error message here

play04:33

which looks a bit weird if you go to

play04:35

this unique point of definition and you

play04:36

actually scroll down a bit you'll see

play04:38

that the copy constructor and the copy

play04:40

assignment operator are actually deleted

play04:42

which is why you get a compile error if

play04:44

you try and do something like this and

play04:45

that's that's there specifically to

play04:47

prevent you from digging yourself into a

play04:49

grave because you cannot copy this

play04:50

because because again as soon as one of

play04:52

these unique pointers dies they all

play04:54

essentially kind of die because the

play04:55

memory the underlying memory of that

play04:57

peepal gated object gets freed so if you

play05:00

like sharing that's where shared pointer

play05:02

comes in and share pointer kind of works

play05:04

a bit a bit differently it's a bit more

play05:06

hardcore if you will because it does a

play05:07

lot of other stuff under the hood the

play05:09

way that a shared pointer is implemented

play05:11

is actually kind of up to the compiler

play05:12

and the standard library that you're

play05:13

using with your compiler however in

play05:16

pretty much all systems that I've seen

play05:17

it's it's using something called

play05:18

reference counting we're gonna have a

play05:20

specific video about reference counting

play05:22

and in fact a lot of these things in the

play05:23

standard library we're actually gonna

play05:25

have videos where we implement them

play05:26

ourselves because they're great examples

play05:28

of how C++ works and how we can kind of

play05:30

use C++ so we're going to definitely

play05:32

write our own unique bonus montoya

play05:34

shared point all of that kind of stuff

play05:36

in the future as well as other kind of

play05:38

satellite bury features so if you if you

play05:40

want that that's coming but the way the

play05:42

shared pointer works is via reference

play05:44

counting and reference counting is

play05:45

basically a practice where you keep

play05:46

track of how many references you have to

play05:49

your pointer and as soon as that

play05:50

reference count reaches zero that's when

play05:52

it gets deleted so as an example I

play05:54

create one shared pointer I then create

play05:57

another shed pointer and copy that my

play05:58

ref count is now two so one for the

play06:00

first one once the second one that's two

play06:01

when the first one dies my reference

play06:03

count goes down one so I'm on one now

play06:05

and then when the last one dies my

play06:07

reference count goes back to zero and

play06:08

I'm dead

play06:09

so the memory gets freed so to use a

play06:11

shared pointer you just type in STD

play06:13

shared point I'll I'm just get rid of

play06:15

this compile error will do NC over here

play06:18

as the template parameter

play06:19

I'll do shared entity as the

play06:22

and I'll set this equal to STV make

play06:24

shared MT now in this case you could

play06:27

have also done a new entity like this

play06:30

and you can see if that compiles fine

play06:32

except you definitely don't want to do

play06:33

that with shared pointer with unique

play06:35

points are really the only reason not to

play06:36

call new directly is because of

play06:38

exception safety built with shared

play06:39

pointer there's actually going to be a

play06:40

difference because shared pointer has to

play06:42

allocate another block of memory called

play06:44

the control block where it stores that

play06:45

reference count and if you create if you

play06:48

first created a new entity and then pass

play06:51

it into the shared pointer constructor

play06:53

it has to allocate that's test2

play06:54

allocation that's right because you

play06:56

constructing the entity first and then

play06:57

be shared pointer has the controller cut

play06:59

it has to construct its control block

play07:01

whereas if you do make share it can

play07:02

actually construct them together which

play07:04

is a lot more efficient and also for

play07:07

those of you people who hate new and

play07:09

delete this obviously gets rid of the

play07:11

new keyword from your codebase because

play07:12

you're just calling a city make shared

play07:14

instead of new entity so I bet you guys

play07:16

love that

play07:16

so with shared pointer you can of course

play07:19

copy it and yeah I mean if I type in

play07:23

code like this it's gonna work perfectly

play07:24

fine I could also move this outside to

play07:26

here and just have this over here let's

play07:28

let's make another scope for fun so I

play07:30

can kind of demonstrate how this works

play07:31

and you drag this over here alright so

play07:34

I've got kind of two scopes again the

play07:35

first one I've got is 0 that's it and

play07:37

then in this one I have my shed entity

play07:39

I'm going to assign easier with my shed

play07:41

entity I'm just going to comment out all

play07:44

get rid of all that other code with the

play07:45

unique pointer so now what will happen

play07:47

is if I hit f5 the first thing that's

play07:49

gonna happen is I'm going to construct

play07:51

my entity so that's done good it's

play07:53

created I'm going to assign this when

play07:56

the first coat dies this challenge she

play07:57

dies however you can see that it hasn't

play08:00

destroyed my HT it hasn't deleted it

play08:02

because a 0 is still alive at holding a

play08:04

reference to that entity now when I have

play08:07

10 that's when it dies when all the

play08:09

references are gone when all of the

play08:11

stack allocated kind of objects that

play08:13

keep track of all the shared pointers

play08:15

when they die when they get read from

play08:17

memory all of them that's when your

play08:19

underlying entity gets deleted all right

play08:21

and finally there's something else that

play08:22

you can use with shared points are

play08:24

called

play08:24

a weight pointer and what you can do

play08:26

with that is just declare it like it was

play08:28

anything

play08:28

and kind of give it the value of a share

play08:31

density and what this does what this

play08:34

does is kind of the same as if you were

play08:36

to copy that share entity and increase

play08:38

the ref count except it doesn't

play08:39

including it doesn't increase the ref

play08:40

count when you assign a shared pointer

play08:42

to another shared pointer thus copying

play08:44

it it will increase the ref count but

play08:46

when you assign a shared pointer to a

play08:47

weak pointer your orange increase the

play08:49

ref count so this is great for if you

play08:50

kind of don't want to take ownership of

play08:52

the entity like you might be storing a

play08:54

list of entities and you don't really

play08:55

care if they're valid or not but you

play08:57

just want to store like a reference to

play08:58

them right with weak pointer you can

play09:00

kind of ask it hey is this is this still

play09:02

even alive and if it is you can do

play09:03

whatever you need to do but it won't

play09:05

keep it alive because it doesn't

play09:07

actually increase the ref count meaning

play09:08

that if I worst was to actually replace

play09:10

this shed and see where the weak pointer

play09:12

and then basically did the exact same

play09:15

thing that I did before then the entity

play09:17

will get credit here

play09:18

it'll get assigned to the shed and see

play09:19

but when I exit the first car that is

play09:22

what it gets destroyed so this weight

play09:24

pointer is now pointing to an invalid

play09:25

and T however you can ask a weak pointer

play09:28

are you expired

play09:29

are you still valid so that's pretty

play09:30

much smart pointers now as for when you

play09:32

should use them you should probably try

play09:34

and use them all the time if I'm being

play09:36

completely honest they automate your

play09:37

memory management they they they get rid

play09:39

of all they did they prevent you from

play09:41

accidentally leaking memory by

play09:42

forgetting to call delete they're really

play09:44

quite useful shared pointer specifically

play09:46

has a bit of an overhead because of its

play09:48

reference counting systems but then

play09:49

again a lot of people a lot of people

play09:50

who tend to write their own memory

play09:52

management systems tend to have a bit of

play09:53

an overhead as well so it's kind of it's

play09:56

a very delicate topic because you have

play09:58

this new breed of C++ programmers now

play09:59

who only use these kind of features and

play10:01

then you have like the optical people

play10:03

who using you and delete I'm kind of a

play10:05

bit of both because there is a time

play10:07

where you want to use unique pointer or

play10:09

shared point two perhaps but there's

play10:11

also a time where you need new and

play10:12

delete so don't think that this has

play10:14

completely replaced you in delete in my

play10:15

opinion it has absolutely not replaced

play10:17

new and delete it's just something that

play10:19

you should probably do when you just

play10:20

need to declare a heap-allocated object

play10:22

and you don't particularly want to tidy

play10:23

up after yourself because you don't need

play10:25

to kind of explicitly call to later

play10:27

explicitly manage that memory in those

play10:29

cases you should be using a smart

play10:30

pointers usually use unique pointer

play10:32

whenever you can because it has a lower

play10:34

overhead but if you absolutely need to

play10:36

share between objects so you just can't

play10:38

use any

play10:39

just can't use a unique pointer then use

play10:41

a shared pointer but definitely going

play10:43

that order first you think pointer first

play10:44

preference share point a second

play10:46

preference hope you guys enjoy this

play10:47

video if you did even if that like

play10:49

button on our YouTube check I said it

play10:51

was like the said it was there last time

play10:53

but now it's like in the middle it's

play10:54

like here or something out in our anyway

play10:56

you can click that like button if you

play10:58

enjoyed this video leave any suggestions

play11:00

you have for future videos and all that

play11:01

in the comment section below as well as

play11:03

any questions you may have

play11:04

I have a discord server where you can

play11:06

also ask questions link in the

play11:07

description below and if you really like

play11:09

this video and you like this series and

play11:11

you want to support it you want to see

play11:12

more videos then you can go to

play11:13

patreon.com/scishow turner pick up some

play11:15

sweet rewards by helping to support this

play11:17

series I will see you guys next time

play11:19

goodbye

play11:20

[Music]

Rate This

5.0 / 5 (0 votes)

Related Tags
Smart PointersC++Memory ManagementUnique PointerShared PointerReference CountingException SafetyStack AllocationHeap AllocationProgramming Tutorial