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

Поділитися
Вставка
  • Опубліковано 30 лип 2024
  • 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 ⬇️
    C++ Lambdas e-book - free download here: bit.ly/freeCppE-Book
    Entire Object-Pascal step-by-step guide - free download here: bit.ly/FreeObjectPascalEbook
    🚀📈💻🔥 My Practical Programming Course: www.codebeautyacademy.com/
    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.
    Download Visual Assist here: bit.ly/WT-CB
    I use it to enhance the performance, features, and support for C, C#, and C++ development in Visual Studio.
    Download Ultra Edit here: bit.ly/UE-CB
    It is a powerful, secure text editor designed specifically for programmers.
    ☕ If you've found my content helpful and would like to support me, you now have the option to buy me a coffee or a cookie! It's a small gesture of gratitude that means a lot to me and helps me keep creating free educational videos for you. Use the link to make a contribution: bit.ly/CodeBeauty_BuyMeACoffee
    However, please don't feel obligated to do so. I appreciate every one of you, and I will continue to share valuable content with you regardless of whether you choose to support me in this way. Thank you for being part of the Code Beauty community! ❤️😇
    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++
    How to install and use PVS-Studio - • How to detect errors a...
    Object Oriented Programming - bit.ly/Cpp_OOP_Playlist
    C++ Pointers - • C++ POINTERS FULL COUR...
    Learn Exception handling - • Exception handling in ...
    Tag me on you Instagram stories:
    Instagram 📸 - / truecodebeauty
    Twitter 🐦- / truecodebeauty
  • Наука та технологія

КОМЕНТАРІ • 225

  • @CodeBeauty
    @CodeBeauty  3 роки тому +26

    📚 Learn how to solve problems and build projects with these Free E-Books ⬇️
    C++ Lambdas e-book - free download here: bit.ly/freeCppE-Book
    Entire Object-Pascal step-by-step guide - free download here: bit.ly/FreeObjectPascalEbook
    🚀📈💻🔥 My Practical Programming Course: www.codebeautyacademy.com/
    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
    #include
    using namespace std;
    class MyClass {
    public:
    MyClass() {
    cout

  • @Ghisisan_
    @Ghisisan_ 3 роки тому +48

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

  • @mahdibiabanpour7691
    @mahdibiabanpour7691 2 роки тому

    Great job with different components explaining different types. The pace is very good and it lets the information settle in mind rather than rushing to write some code and wrapping up the video!

  • @chandranamani8469
    @chandranamani8469 3 роки тому +4

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

  • @anjalikona4487
    @anjalikona4487 Рік тому +2

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

  • @codenote202
    @codenote202 3 роки тому +47

    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 ❤️

    • @CodeBeauty
      @CodeBeauty  3 роки тому +6

      ❤️❤️

    • @markknight5178
      @markknight5178 Рік тому

      I often find myself going back and rewatching her videos just because of how thoroughly she explains things.

  • @mehmethilmiemel8937
    @mehmethilmiemel8937 3 роки тому +18

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

    • @CodeBeauty
      @CodeBeauty  3 роки тому +2

      You're welcome! That's great to hear! ❤️

  • @miramuro9528
    @miramuro9528 3 роки тому +1

    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!

  • @bimblebom
    @bimblebom 6 місяців тому +1

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

  • @mjshastry7531
    @mjshastry7531 2 роки тому

    Hey Saldina! Hope you're doing well.
    Great video, I really appreciate that you are making this kind of content and I feel so glad that I bumped into your channel quite a while ago.
    And to answer your question in the video, YES, please teach me (and your audience) how to work with C#. It would be really cool to learn from a person like you...
    I already enjoy your C++ videos very much (as nobody managed to clear the finicky doubts in my head as much as you could) and I very much look forward to a playlist with videos on C#.
    Take care...
    With warmest regards,
    Your admirer.

  • @majdmansour1876
    @majdmansour1876 2 роки тому +4

    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!

  • @zepplondon
    @zepplondon Рік тому

    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

  • @exploring_axomia
    @exploring_axomia Рік тому +1

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

  • @chip4683
    @chip4683 4 місяці тому

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

  • @kareemgameel150
    @kareemgameel150 3 роки тому +2

    very Helpful Content i recommended your channel to my friends and My Group Community .. keep going Mrs. Saldina 💻

  • @nicholasoneal1521
    @nicholasoneal1521 3 роки тому +1

    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 UA-cam
    Edit:
    24:14
    I expect that wePtr1 is going to be equal to NULL

  • @kayahantasyaran6310
    @kayahantasyaran6310 3 роки тому +1

    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.

  • @shritishaw7510
    @shritishaw7510 2 роки тому +1

    Honestly, all you explained in this video are phenomenal pieces of information but the way you explained it to us is marvellous. Please keep up this good work. Thanks a lot for the precise yet effective video.

  • @nickyrosenberg2285
    @nickyrosenberg2285 2 роки тому +1

    Thanks for your videos. They're very clear and easy to follow.
    And also you're smarter than a smart pointer :)
    Can you please make some videos about Lambda function, Template function and mutable vs const ? Thanks :)

  • @meosart1758
    @meosart1758 2 роки тому +3

    C# videos would be great, thanks!

  • @unpersonownlife
    @unpersonownlife 3 роки тому +11

    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

    • @StarFury2
      @StarFury2 Рік тому

      Actually I can't even console out smart pointer using

  • @antoniotonisi2419
    @antoniotonisi2419 2 роки тому

    Thank you so much for the high quality videos.

  • @nikhilchourasiya4720
    @nikhilchourasiya4720 Рік тому

    Great job
    you always increase my information in cpp
    Thanks a lot
    keep uploadind video related to cpp advanced topics

  • @ynny7885
    @ynny7885 Рік тому

    Very well explained. Thank you and happy new year.

  • @cococoding1430
    @cococoding1430 3 роки тому +6

    Watching from PH 🇵🇭

    • @CodeBeauty
      @CodeBeauty  3 роки тому +2

      Greetings for you! 🇵🇭❤️

  • @imranshah5382
    @imranshah5382 2 роки тому +1

    liked the detailed explanation of concepts and different aspects

  • @tirushsanju2917
    @tirushsanju2917 2 роки тому +1

    Saldina ,Thank you help to improve my knowledge. I am proud of you 🎉️🎉️🎉️🏆️

  • @CodeBeauty
    @CodeBeauty  3 роки тому +20

    C# like, JavaScript comment

  • @vseimenazabiliblin
    @vseimenazabiliblin 3 роки тому

    Hi Saldina,
    Thank you very-very much for your great work on this channel!
    I will be appreciate if you will make some lesson about multi-threading.

  • @gangadharpabbineedi577
    @gangadharpabbineedi577 Місяць тому

    This is so good! Thanks a lot!

  • @oscarquiroz1904
    @oscarquiroz1904 Рік тому +1

    Amazing explanation, thanks!

  • @rakshitsen5110
    @rakshitsen5110 3 роки тому +9

    Great explaination ma'am

    • @CodeBeauty
      @CodeBeauty  3 роки тому +1

      Thanks for liking 😃❤️

    • @rakshitsen5110
      @rakshitsen5110 3 роки тому

      @@CodeBeauty no ma'am thanks for making this video ☺.

  • @srinadhp
    @srinadhp 2 роки тому +1

    Thanks for a very detailed explanation. Very helpful!

  • @andresnaples
    @andresnaples 3 роки тому

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

  • @sudhakarjawake2355
    @sudhakarjawake2355 2 роки тому

    Great explanation. Thank You.

  • @gjhuang608
    @gjhuang608 3 роки тому

    very nice teacher! Thank you!

  • @astudent797
    @astudent797 3 роки тому +7

    It will be great if you mam teach us both or C#

    • @CodeBeauty
      @CodeBeauty  3 роки тому +7

      So C# first and then JS 😁😁

    • @astudent797
      @astudent797 3 роки тому +3

      @@CodeBeauty thank you for considering both

  • @sbsyr5555
    @sbsyr5555 2 роки тому

    Excellent Analysts... 👍

  • @jonathaniurman9794
    @jonathaniurman9794 Рік тому +1

    So clear now! you saved me, thanks a lot!

  • @wjc1514
    @wjc1514 2 роки тому

    thank you, i finally know how the weak pointer is working

  • @yrds96
    @yrds96 3 роки тому

    Thanks for the well explained video. I am developing a program with intent to be memory safe and smart pointers is the way
    Also, could you please make a video about chrono(date and time)? I've read some documentation but i cannot figure out how to efficiently work with date and time with c++

  • @ApoorvSaxena8
    @ApoorvSaxena8 3 роки тому

    Very useful content regarding c++ on whole internet

  • @aquadap219
    @aquadap219 Рік тому

    this is the best channel omg

  • @nastaranhendijani681
    @nastaranhendijani681 4 місяці тому

    Thank you!

  • @koustav2008
    @koustav2008 3 роки тому +1

    EXCELLENT !!!

  • @moizsultan5440
    @moizsultan5440 3 роки тому +1

    When i started watching your vids you barely had 5k subscribers but your hard work payed off Saldina 78k wew congratulations keep growing up

    • @CodeBeauty
      @CodeBeauty  3 роки тому

      I remember 🤗 Thank you! 🙏💙

  • @kuldeeprane1469
    @kuldeeprane1469 3 роки тому +12

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

    • @ghislain7282
      @ghislain7282 3 роки тому +4

      I would like this as well, thank you mate

    • @CodeBeauty
      @CodeBeauty  3 роки тому +11

      A few years ago I was scouted and invited to work for Google. I can share my experience related to the knowledge that was required to get a Google Software Engineer job. 😀😀

    • @kuldeeprane1469
      @kuldeeprane1469 3 роки тому

      @@CodeBeauty yes mam it will be helpful for us

  • @aqdaszulfiqar443
    @aqdaszulfiqar443 3 роки тому

    Hey u really make my CS degree easy and understandable. Can u please make a video on simple c++ projects or games ?? Atleast one project
    I just want to understand how can we make easy console games in c++ .

  • @vijayd595
    @vijayd595 2 роки тому

    Hey Saldana ...I'm impressed with ur teaching...pls make session on C++ STL

  • @zc7504
    @zc7504 2 роки тому

    LUVE this!!

  • @reina4969
    @reina4969 2 роки тому

    I love the change to dark background.

  • @clau9949
    @clau9949 3 роки тому +2

    Would be great to make C# videos!

  • @djordandjordanarieff2205
    @djordandjordanarieff2205 Рік тому

    you just gained a new follower

  • @mirok2000
    @mirok2000 2 роки тому

    Thanks for the video. it is nicely done. Just to clarify, in the first example, I guess the destructor would be called regardless of using smart pointer or not? !!

  • @engineer6525
    @engineer6525 3 роки тому

    thank you very much for your effort really it is not easy but can you explain how we can use them in our live programming. second
    can you ,if your time is available, explain how I can get the cursor (mouse) position from monitor and use it during the program running.

  • @prashanth7641
    @prashanth7641 10 місяців тому

    O my goddess,
    Lovely tutorial.
    Your tutorial is as beautiful as you are.
    Thank you Saldina. Lots of love 💖 from Indian C++ Developers.

  • @nickchasiotis2058
    @nickchasiotis2058 3 роки тому +1

    you are doing a great job. Thanks for your videos. Take care of your self, do not hit your arms again......

  • @alanmichaelthayil967
    @alanmichaelthayil967 2 роки тому

    thank you :)

  • @robertowolfo2128
    @robertowolfo2128 2 роки тому

    Would love a video on c++ move operations.

  • @akindomugisha4644
    @akindomugisha4644 3 роки тому

    Yes please, do lessons for C# (sharp) : )

  • @moularaoul643
    @moularaoul643 3 роки тому +4

    Thank you!!!

  • @felipe_ai
    @felipe_ai 3 роки тому +4

    Otro excelente video, eres la mejor profesora de C++ en UA-cam :)

    • @CodeBeauty
      @CodeBeauty  3 роки тому

      Te lo agradezco mucho! 😃😃

  • @ajaygunalan1995
    @ajaygunalan1995 Рік тому

    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.

  • @sadbinmohshin7564
    @sadbinmohshin7564 2 роки тому

    Hey codebeauty can you plz tell me which book you use to learn to code beautifully ? 🙂🙂

  • @thaboleroytheosmketsu8864
    @thaboleroytheosmketsu8864 3 роки тому +2

    Please do create C# videos that would be a great help

  • @user-lg5bx8jl3z
    @user-lg5bx8jl3z 3 роки тому

    Can you tell sth about difference between weak ref and strong ref? And when should we use weak pointer? Thx

  • @jackd542
    @jackd542 3 роки тому

    What are your thoughts on the Eclipse IDE if you know about it?

  • @agnesakne4409
    @agnesakne4409 2 роки тому

    this video is good

  • @mahadifamgate2686
    @mahadifamgate2686 2 роки тому

    Please make some video on C# specially advance topic such as Events, Delgegate, AppDomai and it's Design pattern things as they as hard part for home alone.

  • @johnkgeorge5263
    @johnkgeorge5263 3 роки тому

    Pl. consider giving separate serial nos for each session (eg:Cpp001) along with the description

  • @willrose6182
    @willrose6182 2 роки тому

    Something I don't really understand though is how to pass smart pointers to other functions - I know I can pass them the same way as a raw pointer, but I've heard passing by reference is preferred. So if I defined a smart pointer (lets say a unique ptr) and then dereferenced it and passed the dereferenced value to a function which was taking a reference, would this then break the smart pointer or is that fine? Can I then assign to this reference? Thank you for the video though!

  • @patrickdavid5961
    @patrickdavid5961 3 роки тому +3

    Hi Saldina, I really love your videos and I did learn a lot about C++, thanks a lot for what you do for all of us.
    Questions about smart pointers, OK I understand the concept... but when and in which occasion to use them? Could you explain in which occasion we should use them because to be honest I really don't know... maybe I should have use them but for me it is not obvious in which occasion I should use them.
    Do you have examples showing in which occasion they should be used?

  • @haroldthibault9921
    @haroldthibault9921 2 роки тому

    Hi from France.
    I've been watching quite some of your videos.
    That is the first one I got lost.
    I guess, the reason is I don't see what advantages smart pointers allow and in what situation where to use them.

  • @mehmetacar5881
    @mehmetacar5881 3 роки тому +1

    Hi saldina,
    i want to ask you a question.
    When i write the unique_ptr code in line,after line i cant use cout

  • @saifmos6516
    @saifmos6516 2 роки тому

    Wait a bit but if you delete system("pause>nul"); you will be able to have "Destructor invoked" so that was the problem right ?

  • @cheetahalpha4648
    @cheetahalpha4648 3 роки тому +2

    Plz make some more videos about structure data

    • @CodeBeauty
      @CodeBeauty  3 роки тому +1

      Data structures or structured data 😁😁

  • @RK-vm6eu
    @RK-vm6eu 3 роки тому

    what are the difference and similarities between weak pointer and unique pointer same ?

  • @AlexSmith-dh1oz
    @AlexSmith-dh1oz 3 місяці тому

    any problems with doing auto ptr = std::make_unique(69); ? Saying the type twice feels kinda redundant

  • @sverkeren
    @sverkeren 3 роки тому

    At the very end of the video; if the integer 25 was deallocated when shPtr1 goes out of scope how can the debugger read its value to be still 25 (via wePtr1)?

  • @is_2p
    @is_2p 3 роки тому +9

    ❤❤❤

  • @mahdavimail
    @mahdavimail 3 місяці тому

    Hello this video explains smart pointers very well thanks. But it's missing the distinction between smart pointers and regular pointers.
    I feel I can use regular pointers instead. I just can't understand the usecase difference here. Maybe a topic for a future video. Thanks

  • @davidorellana678
    @davidorellana678 3 роки тому +3

    Hello Saldina. How do we know in which situations we need to use regular pointers or smart pointers?

    • @CodeBeauty
      @CodeBeauty  3 роки тому +6

      I'm linking a stack overflow post related to your question. Hope it helps you understand better 😃😃
      stackoverflow.com/questions/6675651/when-should-i-use-raw-pointers-over-smart-pointers

  • @alexisdelacruz6110
    @alexisdelacruz6110 7 місяців тому

    Hi What is **ptr in pointer variable how is it being used in programming c++

  • @granumuse7847
    @granumuse7847 3 роки тому

    As the two shared pointers are created in that scope, after that scope at line 23 we should get null pointer exception. Let's see what really happens...

  • @mrbbayat
    @mrbbayat 2 роки тому

    How did you print the address of the unique pointer? Because when I tried, I got a compilation error that there are no matches for the operator

  • @PabloFranco1000
    @PabloFranco1000 2 роки тому

    Hello, how to make shared_ptr return a different memory address for an X pointer each time it is called?

  • @vikram2975
    @vikram2975 2 роки тому

    You are making very nice video❤ , just one suggestion keep camera focus on face .As audience , our attention goes to unnecessary hand movements

  • @nomesa7374
    @nomesa7374 5 місяців тому

    what is the use of weak_ptr? Just for observation of the dead things?

  • @manoharsagunthalla9215
    @manoharsagunthalla9215 Рік тому

    Dear Saldana where these share pointer etc to be used in real life programs. Please explain with example.

  • @cesaraugustop
    @cesaraugustop 3 роки тому +1

    Her Smile...please, Smile++!, delay(20000); Thanks, really helpful!

  • @HOWTO-sf6sy
    @HOWTO-sf6sy 3 роки тому +1

    Mam I want data structures and algorithms full course in c++ like you have uploaded c++ for beginners full country earlier
    Please mam do it 🙏

  • @chanduchanti8775
    @chanduchanti8775 2 роки тому

    can you explain , how to use unique pointer for files ?

  • @rakshitsen5110
    @rakshitsen5110 3 роки тому +1

    Ma'am please explain SQl tooo please.

  • @chiyang3749
    @chiyang3749 3 місяці тому

    no constructor and destructor calls from weak_ptr because it can not be used to allocate memory

  • @alexfrozen
    @alexfrozen Рік тому +2

    Saldina is not just developer. She is saint developer)

  • @yevheniiboiko8397
    @yevheniiboiko8397 Рік тому

    So us understood, a week_ptr will keep just address the first element of memory, and if we try to get data, which was destroyed before, we will get a trash or null ref exception or segmentation fault?
    I guess we will have a trash. Am I right?

  • @kunalmeshram2323
    @kunalmeshram2323 8 місяців тому

    i have a question when i tried to print memory address of ptr1 it throws me error then i need to add &ptr1 to give me memory address but while watching vid i see you didn't mention &ptr1 but still you got memory add how does it happening i'm also using vs ???????? time 3.44 in vid

  • @quicknik107
    @quicknik107 5 місяців тому +1

    "deleting code is my favorite part of coding" ... YES I AM NOT ALONE!!! when deleting something and the needs are still covered, the world just became a better place 🤣

  • @sashaa8056
    @sashaa8056 Рік тому

    Could you add on ppltasks library

  • @srikanthreddy9330
    @srikanthreddy9330 4 місяці тому

    When we assigned a share pointer to a week pointer then the owners should be two in that scope. But it's showing as only one owner .
    How is that possible?