Тёмный

Destructors in Programming: Practical Demonstration 

CodeBeauty
Подписаться 278 тыс.
Просмотров 31 тыс.
50% 1

📚 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. You can use the link below 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! ❤️😇
👉 In this video, we'll take a deep dive into destructors, a key concept in programming that can help you avoid memory leaks and other bugs. You'll learn what a destructor is, how to follow best practices when working with them, and how to debug common issues. We'll also cover the relationship between pointers and destructors and demonstrate how to fix a memory leak.
Here's what you can expect to learn in this video:
What a destructor is and why it's important in programming
8 essential rules for working with destructors
Tips for debugging and troubleshooting common issues with destructors
How to understand the relationship between pointers and destructors
A practical demonstration of a memory leak and how to fix it
Whether you're a beginner or an experienced programmer, this video is sure to provide valuable insights and tips for mastering destructors in your code.
👉 Download a 7-day free trial from the PVS-Studio website or get an exclusive 30-day fully functional license for free by entering the 'code_beauty' promocode or use this link:
bit.ly/PVS-StudioFreeCB1
Contents:
00:00 - What You'll Learn in This Video
02:08 - What Is a Destructor in Programming?
04:22 - 8 Rules for Destructors
06:57 - Debugging and Practical Demonstration of Destructors
09:18 - Pointers and Destructor Relationship
12:21 - Demonstration of a Memory Leak
15:33 - Why Do We Have This Bug?
16:48 - Practical Programming Course
Related videos to watch:
Constructors video 1: • C++ OOP (2020) - What ...
Constructors video 2: • OOP Constructors - Typ...
Copy constructors: • C++ Copy constructors ...
Pointers playlist: bit.ly/Cpp_Pointers_Playlist
Add me on other platforms for more educational content:
Instagram 📸 - / truecodebeauty
Twitter 🐦- / truecodebeauty
******CODE IS IN THE COMMENTS******

Наука

Опубликовано:

 

10 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 125   
@CodeBeauty
@CodeBeauty Год назад
📚 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. ****CODE FROM THE VIDEO IS BELOW**** #include #include using namespace std; class Book { public: string Title; string Author; int* Rates; int RatesCounter; int* Year; Book(string title, string author) { Title = title; Author = author; RatesCounter = 2; Rates = new int[2]; Rates[0] = 4; Rates[1] = 5; Year = new int; *Year = 10; cout
@johnniewalkerjohnniewalker2459
We need a copy constructor for the book3 object.book3 object is declared and also initialized simultaneously.Firstly book3 will be destroyed but also will be freed the dynamic memory from the year pointer.When book1 goes to get destructed it will not find memory to free so the delete year will not work because the pointer year pointa nowhere.
@nv9usb381
@nv9usb381 Год назад
the best programming teacher ever
@milkamilkica1935
@milkamilkica1935 Год назад
i'm so happy that i fund your channel. you saved my career
@CodeBeauty
@CodeBeauty Год назад
so glad to help
@zacharysawyer5252
@zacharysawyer5252 Год назад
Funny that these are always something that I use when workin with cpp and know what they do but feel it is nice to get an indepth example of what they are actually doing.
@CodeBeauty
@CodeBeauty Год назад
Glad that I managed to provide some insight for someone who already has experience 😊😊
@naderhumood1199
@naderhumood1199 Год назад
You outdid yourself today....Great professor......Thank you very much indeed.
@CodeBeauty
@CodeBeauty Год назад
you're making me feel shy 🤭
@naderhumood1199
@naderhumood1199 Год назад
Lovely channel. Lovely lady.....I've learnt a lot. Thank you so much.
@AravindhSK
@AravindhSK Год назад
Thank you. I learned a lot from your C++ video tutorials. Stay blessed ❤
@ccsiri
@ccsiri Год назад
can you upload video about graph data structure. It is great help for me.❤
@CodeBeauty
@CodeBeauty Год назад
As always, if there are enough requests, I will be more than happy to cover that topic too. In the meantime, I'm trying to focus on topics with higher demand. Stay in touch. ❤
@fatimakamran9996
@fatimakamran9996 Год назад
Mam please create a separate video on constructors
@SZMarcos
@SZMarcos Год назад
The previous video is about that.
@davidmorris2848
@davidmorris2848 Год назад
ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-hBq6EGX5II4.html
@CodeBeauty
@CodeBeauty Год назад
There you go, thank you Marcos
@attaullahbrohi
@attaullahbrohi 11 месяцев назад
I prefer watching your videos than scrolling on social media in my free time, keep making amazing videos❤
@user-eo1qj2np1v
@user-eo1qj2np1v 3 месяца назад
You could use member initialize list so that the compiler won't construct twice. (By the way, thank you for your tutorials, they're excellent.)
@dundyd3612
@dundyd3612 Год назад
thank you for making these videos
@swastikbhandari2644
@swastikbhandari2644 2 месяца назад
best tutor for c++ , very detailed explanation
@CodeBeauty
@CodeBeauty 2 месяца назад
🥰🥰
@jamalbello9077
@jamalbello9077 Год назад
Do you do java please 🙏 in need of help
@CodeBeauty
@CodeBeauty Год назад
Currently I'm focusing on my C# practical programming course and I'm telling you this because it will be focused mostly on programming concepts and developing logic, which is transferable on every programming language, and it will certainly help you in learning Java too, for sure. Here you can subscribe for discount, and I will notify you when the course is out, so don't miss it: bit.ly/SimplifyingCoding
@ertemeren
@ertemeren 8 месяцев назад
Thank you for teaching debugger useage at the same time as well.
@Ghisisan_
@Ghisisan_ Год назад
Another excellent video! Cheers!🤗
@CodeBeauty
@CodeBeauty Год назад
Thank you! Cheers!
@zoro-i
@zoro-i Год назад
you helped me a lot thanks
@marym9003
@marym9003 Год назад
great video, you really shared important info that i didn't know before
@CodeBeauty
@CodeBeauty Год назад
Glad it was helpful!
@l.p.1967
@l.p.1967 Год назад
amazing, thank you
@CodeBeauty
@CodeBeauty Год назад
Glad you liked it!
@adreezy6576
@adreezy6576 8 месяцев назад
i like how you recommended the millionaire fastlane. its one of my favorites 😊
@CodeBeauty
@CodeBeauty 8 месяцев назад
Mine too 😃 I've read that book 3 times so far, in different stages of my life, and every time I get something new and useful from it 🚀🚀
@sujalthakkar2118
@sujalthakkar2118 3 месяца назад
Love from India , i have purchased a course of DSA and this topic was hard for me but now it's crystal-clear. Thanks!
@CodeBeauty
@CodeBeauty 3 месяца назад
I'm happy to hear that. You should definitely enroll in my Practical Programming Course to learn how to build real apps and to get skills required for a job. Fell free to use the discount code from the description of this video for a special discount. 😊 Love for India 🇮🇳 🤎
@milanrai1556
@milanrai1556 Месяц назад
@@CodeBeauty how to enroll ma'am?
@qcnck2776
@qcnck2776 Год назад
At the end: book3 is assigned book2 as a shallow copy. When book2 is deleted, book3 now has no access to the actual members of book2? Great video, BTW 👍
@CodeBeauty
@CodeBeauty Год назад
Yess, correct answer! ✋️
@juniorlucival
@juniorlucival Год назад
but, what's the problem? where is the leak memory? The book3 lost reference but not leak memory in my option. Please help me.
@qcnck2776
@qcnck2776 Год назад
@@juniorlucival I think the problem is that book3 still exists, and when it tries to access its members, it may get garbage values or an error. Secondly, if the object has pointers, if book3 is deleted first, the pointer is the only thing being deleted, and the allocated memory cannot be accessed by book2 or any other object, and is lost. Perhaps someone more knowledgeable than me can clarify.
@juniorlucival
@juniorlucival Год назад
@@qcnck2776 i know that, i do this way. In some cases it can be useful. When a pointer loses its reference, its not a problem if was part of your strategy, and i dont see any memory leaks or issues, even when deleting book2.
@billjohnes9380
@billjohnes9380 9 месяцев назад
When book2 is deleted, book3 has already been deleted, so it cannot have or not have any access because it does not exist at the time of book2 deletion.
@seersl123
@seersl123 Год назад
Correction! Actually "delete Rates" will delete the first element (in this case first int variable in the array). "delete [] Rates" will delete all the elements.
@blackwarrior823
@blackwarrior823 11 месяцев назад
Thankx
@georgiosdoumas2446
@georgiosdoumas2446 9 месяцев назад
There are many other errors in this video, check my comment and the comment from @billjohnes9380
@geosimplem
@geosimplem Год назад
very good way you cover the topics, as for the bug, we should do a deep copy
@CodeBeauty
@CodeBeauty Год назад
Excelent, that's the reason, and to fix it we should implement copy constructor which I will cover in one of my next videos.
@gollygobih6527
@gollygobih6527 Год назад
Best explanation ever. I understand your videos perfectly. Thanks
@CodeBeauty
@CodeBeauty Год назад
You are welcome!
@shamaldesilva9533
@shamaldesilva9533 Год назад
Love your teaching style ❤🎉 , could you do a tutorial series on RUST 😊
@danny12345
@danny12345 Год назад
Thank you
@CodeBeauty
@CodeBeauty Год назад
Welcome!
@bousriabderraouf6696
@bousriabderraouf6696 Год назад
Can I know if you have an estimation about when you will start the programming course? Thanks in advance
@vickymar3836
@vickymar3836 8 месяцев назад
Hi, Thanks for the great video. Can you please clarify one doubt. In 14:25 you have said that using delete instead of delete[] will just deallocate the memory used by Rates (ie Left side of the excel). Logically, using delete should instead deallocate only the first memory cell of the contiguous memory block (ie first element of right side of excel ) right ? Memory leaks will still be there because apart from the first cell of the contiguous memory, other cells have not been deallocated. Thanks!!
@jhon3991
@jhon3991 Месяц назад
@CodeBeauty I can't agree more with @vickymar. How do you think about it?
@abdullahbilal9393
@abdullahbilal9393 3 месяца назад
you are actually helping me a lot to learn oops♥
@MuhammadAbdullah-cl2hc
@MuhammadAbdullah-cl2hc Год назад
Please make a video on Aggregation and composition in C++
@QuizzzME
@QuizzzME Месяц назад
Please can you answer is your course is c++ or c# before I enroll I wanna know thanks 🙏
@kassavagt7393
@kassavagt7393 Год назад
This is probably the third video of yours that I have watched today, and surely the teacher I am looking for! You explain things with details and a lot of "What if's" that keeps things interesting, but the best part is having your human figure in the video looking to the camera, For me, I feel like you are talking to me directly as a teacher, this keeps me a lot more focused in class. Thank you for your lessons, sincerely!
@georgiosdoumas2446
@georgiosdoumas2446 9 месяцев назад
4:00 I think it is not a good practice to do these assignments in the body of the constructor in the body, better do them in the initialization list. Viewers of this video BEWARE , you have to realize that what you see is not high quality code. DO not trust what you see here. Go to some good books.
@mehulshah9027
@mehulshah9027 Год назад
very good...
@proexpertsolution242
@proexpertsolution242 Год назад
I am watching from Bangladesh. And mam i am a great fan of you🥰.
@bebagcreativity3149
@bebagcreativity3149 Год назад
Hi saldina I understand your videos very well ....and can U please make a video about UNIONS....Thank U for helping us 🥺🙏
@orhan10
@orhan10 2 месяца назад
Because of book3 holds the address of book1 and book3 gets deleted book 1 will be deleted too program will find that and you can't delete something that is already deleted there shall be an error, am I right ma'am?
@marwasolh9449
@marwasolh9449 Год назад
Hello from Lebanon 🇱🇧♥️ I wish you make data structure and algorithms tutorial 😢 Thank you❤
@adityasinghrajput7815
@adityasinghrajput7815 Год назад
love from india ma'am your way of explaination is best among all youtubers i love you ma'am❤❤
@CodeBeauty
@CodeBeauty Год назад
Thank You very much. In my days of college, I've learned to value someone who has the skill of transferring knowledge more than just having knowledge so it's always nice to get feedback from someone who appreciates your work. Much love for India
@adityasinghrajput7815
@adityasinghrajput7815 Год назад
@@CodeBeautyI am in college fourth semester and currently learning dsa with you just i have finished the linked list video and it was ♾️❤️ 🙌 .
@adityasinghrajput7815
@adityasinghrajput7815 Год назад
@@CodeBeauty i can see the effort you made to create videos surely appreciated ❤️
@adityasinghrajput7815
@adityasinghrajput7815 Год назад
And i am sure being your student i can get job in a big MNC.
@juniorlucival
@juniorlucival Год назад
I make the same code in my visual studio 2012 and here didn't crash code. Everything fine.
@CodeBeauty
@CodeBeauty Год назад
well even better 😂
@pedrovinicius5128
@pedrovinicius5128 Год назад
Sorry about my ignorance, but where can I find the link of the complete course you mentioned?
@horsthorstmann7921
@horsthorstmann7921 10 месяцев назад
Look in the Video description. It ist the third link.
@user-fy7ev9cf5n
@user-fy7ev9cf5n Год назад
thank you for your videos but please you didn't explain the copy constructor I really like your videos this is one of the most perfect explanations that I've ever seen 🙂
@CodeBeauty
@CodeBeauty Год назад
Copy c-tor video will be out as soon as I find time to edit it, it's already filmed ✌️😁❤️
@user-fy7ev9cf5n
@user-fy7ev9cf5n Год назад
@@CodeBeauty thank you very much
@v-sig2389
@v-sig2389 Год назад
Hello Salina ! I know it is impolite to change the subject, but ... what do you think about the new language that can be used in Unreal instead of C++ ?
@pratham30403
@pratham30403 Год назад
Unreal engine added support for a new language?
@v-sig2389
@v-sig2389 Год назад
@@pratham30403 yes, they added Verse, a simpler language that still offers the power of a language while not needong to recompile the whole editor at each code update.
@atulkrjha
@atulkrjha Год назад
How that pointer is holding the address of an array but the type of the pointer is int not integer array?
@CodeBeauty
@CodeBeauty Год назад
It is possible because pointer is pointing to the first element of the array and that element is of type int, and in addition that FIRST element has the same address as the array itself. Also, please keep in mind, when you are initializing dynamic arrays like this, you also need another variable that holds the size of the array, which is in our case RatesCounter. Neat right? 😊😊
@atulkrjha
@atulkrjha Год назад
@@CodeBeauty Awesome explanation Thanks!
@georgiosdoumas2446
@georgiosdoumas2446 9 месяцев назад
The memory address is represented as a big integer , as an int32_t or a int64_t for bigger systems. So the value of the pointer is just an integer.
@AlphaRocketLeo
@AlphaRocketLeo Год назад
Is there any way to handle multiple classes destructor handle in a single class
@CodeBeauty
@CodeBeauty Год назад
Each class should have its own destructor to handle memory allocation and releasing own resources. I hope that answers your questions. Cheers
@AlphaRocketLeo
@AlphaRocketLeo Год назад
@@CodeBeauty Yes i understand it 🤗
@tkj11.hendrik98
@tkj11.hendrik98 Год назад
Good videos
@CodeBeauty
@CodeBeauty Год назад
Glad you like them! ❤
@PabloSanDev
@PabloSanDev Год назад
Thank you beauty
@bilalysf
@bilalysf Год назад
Would like if you manage to upload lectures on python language
@CodeBeauty
@CodeBeauty Год назад
There is definitely plan to do it in future but in the meantime, I'm working on creating course for practical programming in C#, with full application as you would see it in real world and all requirements that such application might have. Since this course will focus on developing your logic and your thinking as programmer, I can assure you that the programming concepts and skills you'll learn in this course are applicable to any language (Python as well), and I'm confident that you'll find it to be a valuable learning experience. I encourage you to give it a try. Use this link bit.ly/SimplifyingCoding to earn special discount and get notified once I publish it.
@Farzadx-65
@Farzadx-65 26 дней назад
@CodeBeauty
@CodeBeauty 25 дней назад
🥰
@thesoftwareproject5798
@thesoftwareproject5798 Год назад
hey code beauty, nice video. so one question (for the advanced programmers): how do you prevent memory leaks if an exception occurs in the constructor? for example at line 19?
@CodeBeauty
@CodeBeauty Год назад
Good point! So, to prevent scenario where we allocate memory, and exception happens which prevents deallocating memory in destructor, we can use Try-Catch block. We could wrap all memory allocation inside constructor with Try clause, and in Catch we could release it as we do now in destructor. Something like this: try { Rates = new int[2]; Rates[0] = 4; Rates[1] = 5; Year = new int; *Year = 10; } catch (const std::exception&) { delete[] Rates; Rates = nullptr; delete Year; Year = nullptr; throw; } Hope that answers your question. 😊
@thesoftwareproject5798
@thesoftwareproject5798 Год назад
@@CodeBeauty yes, that answers my question. that would work. however, using RAII-compliant smart pointers would provide a better, less cluttered solution?
@thesoftwareproject5798
@thesoftwareproject5798 Год назад
@@CodeBeauty of course, this is off topic. your video is already complete.
@kaiwalyakshirsagar
@kaiwalyakshirsagar 10 месяцев назад
Destructor 7 RULES [ IMP ] 1) Destructor always starts with "~". 2) Destructor and constructor do not have any return type. 3) Name of the constructor and destructor is the same as the class name. 4) Destructor does not require any input parameter. 5) Destructor is a member function of the class. 6) Destructor needs to be placed is always a public member. 7) Multiple constructors are possible in one class, but Destructor should be only one.
@billjohnes9380
@billjohnes9380 9 месяцев назад
The 6th one is wrong. If you need to write a singleton class you should place both the constructor and destructor to the private section. Otherwise, someone else in the program will be able to construct one more instance of the singleton or destruct the existing one.
@AlphaRocketLeo
@AlphaRocketLeo Год назад
and one thing more destructor is necessary to used
@CodeBeauty
@CodeBeauty Год назад
not strictly but it's always a good practice. ✌
@nitika9769
@nitika9769 Год назад
next video link
@georgiosdoumas2446
@georgiosdoumas2446 9 месяцев назад
4:00 I think it is not a good practice to do these assignments in the body of the constructor in the body, better do them in the initialization list. Viewers of this video BEWARE , you have to realize that what you see is not high quality code.
@jorgetorres1670
@jorgetorres1670 Год назад
ok
@billjohnes9380
@billjohnes9380 9 месяцев назад
You explain wrong things. Non-array version of delete expression does not delete the address in the pointer itself: it still deletes the allocated memory at that address. In the case you mentioned and on the platform/compiler you use, there is no difference between the two. Yes, applying a non-array delete expression to the address returned by an array new expression is UB. But in this case of the platform/compiler, the UB looks like is expected by a programmer behavior: nothing unusual occurs. C++ is the language of languages, and no one on Earth knows it completely. Your level is at least 3-5 times lower than it is necessary to explain simple things in C++. You pass string objects to the constructor by value. You does not use initialization in the constructor like that: Book(string const &title, string const &author): Title{title}, Author{author} {cout
@georgiosdoumas2446
@georgiosdoumas2446 9 месяцев назад
Damn , just a few minutes ago, I also made a comment about her not using constructor initialization list! And then I started scrolling through the comments. I guess we do not have to feel sorry for the poor people that watch her videos and have the fake impression that they learn something, they must be total beginners, students of computer science that are struggling with C++ and just want to pass the class. I hope those people are not going to work with C or C++ , and they will go to work with other languages. So tell me what modern books do you recommend? I have read the "Thinking in C++" from the free html version, from 2004, and also an old book from 1998 "Navigating Object Oriented design in C++" , the 1999 book of "C++ FAQs" is another book that I have started reading but not yet finished, along with the 2005 "More Effective C++" (I search in the z_l1brary and found the 11th printing that has errors corrected) , I have also read half of Lipmans "C++ primer" 2012 edition. Not yet the "C++ programming language" from 2014 edition. But I have found a book with good reviews on amazon , " Deciphering Object-Oriented Programming with C++" from 2022, and it seems good. Another one that seems good is the "Professional C++" now on its 5th edition from 2021, I hope to read it next year (because for now I started reading some books on Qt5, for my next developers role, it will by my 2nd position, my 1st position that I started in Oct 2022 is about micro-controllers, using C++98 mainly ) When I started watching the video, I was wondering "will she mention when the destructor should be virtual?" , but the level is too simple , and as you said even at that simple level she presents some things wrong. I am surprised that your comment is not deleted actually!
@billjohnes9380
@billjohnes9380 9 месяцев назад
​@@georgiosdoumas2446 I read books very-very rarely, so I cannot recommend you modern books. However, who impressed me most of all is Scott Meyers, but in December 2015, Meyers announced his retirement from the world of C++. So, all his books and videos are constantly obsoleting every year. The comment is not deleted probably because, from one side, she may have no time to monitor comments for all her videos. From the other side, there are not so many comments for the video, but comments are important to promote the video, so she may feel sorry to delete them.
@waheedkhan3508
@waheedkhan3508 Год назад
listning you from pakistan i love you
@ImSecular
@ImSecular Год назад
Jalsa nahi ho raha hain jo sun rahe ho
@CodeBeauty
@CodeBeauty Год назад
I'm very well, thank you. Love for Pakistan
@waheedkhan3508
@waheedkhan3508 Год назад
i am listning you from pakistan love you
@CodeBeauty
@CodeBeauty Год назад
Greetings for Pakistan ❤
@waheedkhan3508
@waheedkhan3508 Год назад
how are you mem
@CodeBeauty
@CodeBeauty Год назад
good, you? 😂😂
@Relaxingmusic-nk7py
@Relaxingmusic-nk7py Год назад
​@@CodeBeautymam please make vedios on Salesforce with subtitles ❤❤❤🙏🙏🙏🙏
@xamsemahdi3861
@xamsemahdi3861 Год назад
Beautiful honey❤❤❤❤❤ l love you ❤❤❤❤
@CodeBeauty
@CodeBeauty Год назад
@xamsemahdi3861
@xamsemahdi3861 Год назад
Honey give me phone number
@chinthaka7027
@chinthaka7027 5 месяцев назад
Thank you
@xamsemahdi3861
@xamsemahdi3861 Год назад
@CodeBeauty
@CodeBeauty Год назад
Далее
Zlatan embarrasses Speed 😂 #ishowspeed
00:32
Просмотров 8 млн
SMART POINTERS in C++ (for beginners in 20 minutes)
24:32
Object Oriented Programming (OOP) in C++ Course
1:30:26
Dependency Injection, The Best Pattern
13:16
Просмотров 761 тыс.
C++ Pointers - Finally Understand Pointers
15:56
Просмотров 207 тыс.
КРУТОЙ ТЕЛЕФОН
0:16
Просмотров 4,5 млн