SMART POINTERS in C++ (for beginners in 20 minutes)

preview_player
Показать описание
A smart pointer is a container/wrapper for a raw pointer. In modern C++ smart pointers are defined in the std namespace in the memory header file.
One big advantage of smart pointers is that they are responsible for deleting the memory that they use, which means that they automatically deallocate the memory when they go out of scope.
There are three types of smart pointers in C++: unique pointer, shared pointer and weak pointer (std::unique_ptr, std::shared_ptr, std::weak_ptr)

📚 Learn how to solve problems and build projects with these Free E-Books ⬇️

Experience the power of practical learning, gain career-ready skills, and start building real applications!
This is a step-by-step course designed to take you from beginner to expert in no time!
💰 Here is a coupon to save 10% on your first payment (CODEBEAUTY_YT10).
Use it quickly, because it will be available for a limited time.

Contents:
00:00 - Intro
00:47 - What are smart pointers in C++
01:39 - Unique pointer in C++
04:10 - Sharing vs moving a unique pointer in C++
06:44 - Unique pointer automatic memory deallocation
11:42 - Shared pointers in C++
15:09 - Shared pointer automatic memory deallocation
18:42 - Weak pointer vs shared pointer in C++

Tag me on you Instagram stories:
Рекомендации по теме
Комментарии
Автор

📚 Learn how to solve problems and build projects with these Free E-Books ⬇️
Experience the power of practical learning, gain career-ready skills, and start building real applications!
This is a step-by-step course designed to take you from beginner to expert in no time!
💰 Here is a coupon to save 10% on your first payment (CODEBEAUTY_YT10).
Use it quickly, because it will be available for a limited time.

#include <iostream>
#include<memory>
using namespace std;

class MyClass {
public:
MyClass() {
cout << "Constructor invoked" << endl;
}
~MyClass() {
cout << "Destructor invoked" << endl;
}
};

int main()
{
//unique_ptr, shared_ptr and weak_ptr
//uncomment the block that you want

/*{
unique_ptr<MyClass> unPtr1 = make_unique<MyClass>();
}*/


/*{
shared_ptr<MyClass>shPtr1 = make_shared<MyClass>();
cout << "Shared count: " << shPtr1.use_count() << endl;
{
shared_ptr<MyClass>shPtr2 = shPtr1;
cout << "Shared count: " << shPtr1.use_count() << endl;
}
cout << "Shared count: " << shPtr1.use_count() << endl;
}*/


/* weak_ptr<int> wePtr1;
{
shared_ptr<int>shPtr1 = make_shared<int>(25);
wePtr1 = shPtr1;
}*/

system("pause>nul");
}

CodeBeauty
Автор

"deleting code is my favorite part". I LOLed at that one. :)

Ghisisan_
Автор

More I watch Saldina's video more I want to watch. I am assuring you it's not love it's attachment to her pace and the way she explains ❤️

codenote
Автор

Your explanations are clear and concise, making it easy to understand the principles. Many concepts that I didn't understand before became crystal clear after watching your videos. You've done an excellent job.

stearking
Автор

I was always getting confused about smart pointers . You helped me alot. Great

mehmethilmiemel
Автор

Great job! Unfortunately, my experience has shown that many people who deal with smart pointers need to have a rough understanding of how they work under the hood: these are stack-allocated objects that wrap pointer-like logic. This distinguishes them from the classic pointers. For example, the 'operator*' function is overridden, so that (under the hood) the * operation emulates how classic pointer work, by retrieving the REAL pointer the smart pointer OBJECT manages. But, you should always keep in mind that these are objects that refer to allocated data INDIRECTLY. When you stream the pointer using the 'operator<<', the object outputs the value of the pointer (i.e. address) that it manages. Understanding this concept helps people understand how the lifecycle is managed for smart pointers compared to classic pointers.

unpersonownlife
Автор

I really become happy whenever I search a topic and see you teaching that...

chip
Автор

Nobody explains a hard to understand topics in a easy way like you do. THANK YOU 😌 Saldina

anjalikona
Автор

Tu Español tiene un acento muy lindo. Tu eres una señorita muy inteligente, te felicito tus videos son muy buenos.

andresnaples
Автор

You are always strengthening my knowledge in c++ by making videos on key concepts. Your videos are very understandable.

chandranamani
Автор

hi ma'am. I have been watching your videos to refresh my memory in C++, and I must say that you teach so well. thank you for this content!

miramuro
Автор

Great explanation! I'm coming from Java background and was taking it's memory management for granted, but now after learning how c++ handles pointers I'm full of "a ha!" moments. Thx

zepplondon
Автор

I wish I found this video sooner, I can already see how it's useful. TYSM!

jyapp
Автор

Beautiful! beautiful! Short succinct examples and clear short explanations.

bimblebom
Автор

Nice, Video! Summary: When the owner goes out of scope, the memory (object) is destroyed. unique pointers have only one owner. shared pointers have multiple owners and keep the object alive by using counting the owners. Weak pointers are not owners which means they won't keep the object alive.

ajaygunalan
Автор

Hello Saldina.

First of all, this is one of the good introductory material for smart pointers I've ever seen so far. It is very helpful. I just have a small feedback.

Moving unique pointers mentioned in 6:37. It is pretty much like this but a subtle difference under the hood: std::move does just casting so that moving can be done properly with unique_ptr's move constructor.

I told this because after watching that video people can think "Oh using only std::move provides moving and I don't have to think about anything else, thats good!" Thinking in that way can be misleading for further learning progress.

kayahantasyaran
Автор

Thank you so much for this video. I literally was studying a lecture about smart pointers and I did not understand anything at all. But now it's much better! <3 Keep it up :D

majdmansour
Автор

17:44

I think that at the end of the declared scope, the second pointer is going to be de-allocated and use_count() will return 1. Btw, Just saying, this is going to be really useful, I really hated having to try and figure out all the stuff with de-allocating pointers, thanks a lot, best coding channel on YouTube


Edit:

24:14

I expect that wePtr1 is going to be equal to NULL

nicholasoneal
Автор

I loved the way you have explained. Very simple and detail.

exploring_axomia
Автор

Can you make video on the projects a fresher should do to include them in a resume.

kuldeeprane