Pointers and Dynamic Memory in C++ (Memory Management)

An introduction to using dynamic memory in C++.
Concepts:
Why we may need to allocate memory dynamically?
new operator
heap (free store)
pointers to reference dynamically allocated memory
delete operator
garbage
dangling pointers

Пікірлер: 236

  • @imranabdalla5330
    @imranabdalla53306 жыл бұрын

    thanks man, hats off sir. I wonder why we pay our universities and end up learning free on KZread😒

  • @Guest-gy9vp

    @Guest-gy9vp

    4 жыл бұрын

    Because there are stupid people out there want paper signed by University

  • @rajab4187

    @rajab4187

    4 жыл бұрын

    @@Guest-gy9vp hell yeah Why job market wants that fucking papr signed by fucking garbage university

  • @EduInquisitive

    @EduInquisitive

    4 жыл бұрын

    It's because you guys never concentrate during your lecture.

  • @DonSanchezDK

    @DonSanchezDK

    4 жыл бұрын

    Don't know if you are trolling or for real.. Universities exist to guarantee a certain quality standard, if you have a degree there is a guarantee that you meet those standards and know specific things.. There is no standard for people who learn online, they might be geniuses, but could also be "garbage" programmers.. ;)

  • @ahmedegymed5853

    @ahmedegymed5853

    3 жыл бұрын

    @@DonSanchezDK but as an employer you can make interviews or tests ,if the employee pass ....that is a proof he know things necessary for the job...so there goes the purpose of college !

  • @Neothejew
    @Neothejew7 жыл бұрын

    at 6:48 he crack me up every time. Describing all the code I've ever written.

  • @noobfighter5717

    @noobfighter5717

    4 жыл бұрын

    hahahahahahhaha

  • @cameronsluiter323
    @cameronsluiter3237 жыл бұрын

    Very informative! Really liked your drawing of the 'heap' and the 'stack'. Made it much easier to understand what is going on.

  • @tonyadearaujo
    @tonyadearaujo11 жыл бұрын

    I've been reading Dr. Bjarne Stroustrup's book on Programming and referring to your videos often. I must say that I wouldn't probably be able to go through that complex book if it weren't for your clear, really lucid explanation of concepts. Thank you so much!

  • @jasonitsme
    @jasonitsme3 жыл бұрын

    Super clean and easy explanation. Absolute masterpiece if anyone wants to understand pointers, dynamic memory allocation, stack and heap.

  • @chachamarwa6047
    @chachamarwa60476 жыл бұрын

    i ‘ve watched many best tutorials but this one made my way clear that i won’t ever forget how stuffs are allocated in the memory. Thanks sir

  • @hassanbentiba4504
    @hassanbentiba45044 жыл бұрын

    Just started programming and c++, memory was the first topic that I would first struggle to understand and get my head around and this video really helped :) thank you so much

  • @BrianBurtner
    @BrianBurtner5 жыл бұрын

    This video is 7 years old and it is the best explanation I've yet encountered on this topic. Thanks.

  • @ayeyo4081
    @ayeyo40814 жыл бұрын

    This was the best educational video of explaining pointer and dynamic memory. Everything about pointer and dynamic memory is so much clearer now. Cheers !

  • @g14f
    @g14f Жыл бұрын

    Your efforts are paying off for more than 10 years. This video helped me a lot. Thanks !

  • @irem2719
    @irem27198 ай бұрын

    I have never seen anyone explain this more clearly in my life. You are amazing!

  • @harshitakitchen
    @harshitakitchen4 жыл бұрын

    DUDE I literally got a 100 on my exam because it was based off of memory diagram questions. Thank you!

  • @bdiwy6187

    @bdiwy6187

    3 жыл бұрын

    Nice 👍🙂

  • @sharonjoe7535

    @sharonjoe7535

    3 жыл бұрын

    @Abhinav Rao LMAO

  • @hemiacetal1331

    @hemiacetal1331

    3 жыл бұрын

    @Abhinav Rao bruh moment

  • @eien7228

    @eien7228

    2 жыл бұрын

    @Abhinav Rao do u think people should work infront of the computer 24/7 to use their programming knowledge?

  • @abhishektiwari7643

    @abhishektiwari7643

    Жыл бұрын

    @Harsita's Kitchen do you code while cooking?

  • @Kazner0h
    @Kazner0h11 ай бұрын

    Those illustrations were SOOOOO helpful. Thanks!

  • @MyLittleMagneton
    @MyLittleMagneton8 жыл бұрын

    Since C++11 You should not set a pointer to NULL, but rather to "nullptr".

  • @chcipnifasisto

    @chcipnifasisto

    8 жыл бұрын

    +Peterolen or use 0 and use it in c++1002059

  • @zoranjovanov8564

    @zoranjovanov8564

    8 жыл бұрын

    =0 isjust fine anyway.

  • @MyLittleMagneton

    @MyLittleMagneton

    8 жыл бұрын

    Zoran Jovanov Not really. It was replaced for a reason. Imagine having two functions, void Func(string* aStr) and an overloaded version called void Func(int aNum). If I call Func(0) or Func(NULL), Func(int aNum) will be called. But if we call Func(nullptr), then Func(string* aStr) will be called, which we want as we're dealing with pointers.

  • @chcipnifasisto

    @chcipnifasisto

    8 жыл бұрын

    NULL is just constant of 0 difference is NULL is defined with #define and nullptr is a const variable it was replace just to tell you that you are assigning it to pointer and to avoid some memory leaks :)

  • @zoranjovanov8564

    @zoranjovanov8564

    8 жыл бұрын

    Scias yes but i ment only when u delete pointer so it wont be dangling =0 is just fine.

  • @fireflynahian6878
    @fireflynahian68784 жыл бұрын

    finally understood the concept...this video helped me a lot...

  • @TA-vm8es
    @TA-vm8es10 жыл бұрын

    This helps me to understand the operations of 'new' and 'delete' . Thanks.

  • @blakewhitlock2307
    @blakewhitlock23074 жыл бұрын

    I was debating dropping my major until i found your videos, thanks for the help/motivation!

  • @bodduavinash1
    @bodduavinash110 жыл бұрын

    i got the clear idea of what is heap and how to handle new/delete operators and that too dangling pointers and garbage collection. Thanks for this lecture.

  • @muthu6041
    @muthu60415 жыл бұрын

    He is so simple in explaining complex concepts. thank you

  • @AnmolSharma293
    @AnmolSharma29311 жыл бұрын

    Taking reference from your videos and C++ Primer both, best learning combo ever!

  • @steve018574
    @steve0185746 жыл бұрын

    I love the drawings! You helped me understand what a stack frame is! Thanks!

  • @alive4metal453
    @alive4metal4537 жыл бұрын

    This is a great video, the drawings really help to make the concepts and idea take concrete form for a begginer

  • @alexanderjack8724
    @alexanderjack872410 жыл бұрын

    Excellent video. You really capture the big ideas well.

  • @JO-ct6dl
    @JO-ct6dl2 жыл бұрын

    Excellent explanation with the graphics. Please make more videos. Thank you

  • @avanishpatel4936
    @avanishpatel49366 жыл бұрын

    One of the best explained topic I ever seen

  • @_._lis_._
    @_._lis_._2 жыл бұрын

    Woww😲 had been struggling with this topic for months.. well explained..Tysm.. you're awesom!!!

  • @fizzanasir572
    @fizzanasir5723 жыл бұрын

    Such an amazing amazing amazing amazing amazing amazing tutorial...hats off

  • @icemotion1925
    @icemotion19253 жыл бұрын

    Thank you so much bro! Your drawing and interpretation of heap & stack made me understand the whole thing :D

  • @FluffyNinjaUnicorn3Gaming
    @FluffyNinjaUnicorn3Gaming3 жыл бұрын

    Very informative video! Thank you so much!

  • @War-Cry
    @War-Cry8 жыл бұрын

    Thank you so much for the tutorial. Dynamic Memory Allocation has been kind of a plateau in my C++ learning, and you explained it very concisely. Very well done.

  • @jiroquijano
    @jiroquijano11 жыл бұрын

    Thanks for the video! It's a lot easier to program codes in c++ using memory allocation with your explanation visuals in mind! Thanks a lot! :D

  • @ASDFG856
    @ASDFG8569 жыл бұрын

    best pointer explanation ever :D

  • @lahiru1k
    @lahiru1k9 ай бұрын

    very precise and informative!!~ thank you so much!!

  • @michaelwright8576
    @michaelwright85763 жыл бұрын

    Wow. explained in such simple terms. Thank you so much.

  • @rajcodes100
    @rajcodes1003 жыл бұрын

    This was crystal clear .Thanks

  • @oaomybb
    @oaomybb2 жыл бұрын

    very clear clarification. Thanks man

  • @marianaql
    @marianaql6 жыл бұрын

    I loved your explanation. Thank you!

  • @henrikaskancys4956
    @henrikaskancys49564 жыл бұрын

    Short and informative, thank you!

  • @rogerhunter313
    @rogerhunter3133 жыл бұрын

    This was a great explanation. Thank you!

  • @louischvs9395
    @louischvs93956 жыл бұрын

    The best video I've ever watched about pointers

  • @mukhtarthechosenone7851
    @mukhtarthechosenone78513 жыл бұрын

    Thank you very much. You really helped me. May God guide you and increase your knowledge .

  • @hongminwang6747
    @hongminwang67478 жыл бұрын

    Thank you so much. I watched a few of your videos today, and I found them very helpful. Clear and concise, really impressive!

  • @yapayzeka
    @yapayzeka4 жыл бұрын

    a legendary explanation. thank you sir.

  • @amiralihosseinzadeh7336
    @amiralihosseinzadeh73362 жыл бұрын

    the best 13 minutes of my life

  • @BelowHorizon
    @BelowHorizon10 жыл бұрын

    I love your tutorials. You explain thing soooooooooooo clear. Thank you soooooooooooooooooooooooo much.

  • @anoobis8674
    @anoobis86746 жыл бұрын

    Very informative video! Keep it up.

  • @badis23
    @badis237 жыл бұрын

    thank you so much I never get to understand pointer as I did this time, thank you again

  • @RockSmithStudio
    @RockSmithStudio11 жыл бұрын

    You are incredible! Great presentation! Keep it up!

  • @FriedaNgaharjo
    @FriedaNgaharjo11 жыл бұрын

    Thank you! You really explained it well and it helps me a lot in studying :)

  • @mohammedkashifqamer8665
    @mohammedkashifqamer8665 Жыл бұрын

    Thanks for such an easy explanation

  • @francisDr4ke
    @francisDr4ke4 жыл бұрын

    very great explaination. thank you

  • @Quancept
    @Quancept4 жыл бұрын

    Thanks man, really good explanation!

  • @AyaNori
    @AyaNori2 жыл бұрын

    great job! thanks so much for making this video :) helped me with my homework

  • @BrianVandrian
    @BrianVandrian11 жыл бұрын

    Thanks for the video its NUTS & BOLTS :). I am trying to learn how to make a program and everyone says C++ is powerful because of memory management, but now that I am learning it they keep getting away from the nuts and bolts and pretty soon everyone is talking about packaging everything into things already programmed in other things... boxes within boxes and which box to use... Before too long I don't even know what nuts and bolts are actually in the boxes or anything from anything. So thanks

  • @purushothamreddy4453
    @purushothamreddy44537 жыл бұрын

    Awesome explanation

  • @foreveralone4813
    @foreveralone48138 жыл бұрын

    Thank you so much for offering high quality tutorials!

  • @faris899

    @faris899

    8 жыл бұрын

    +Forever Alone Studying for the CS final? lol

  • @foreveralone4813

    @foreveralone4813

    8 жыл бұрын

    What?

  • @KG_BM

    @KG_BM

    8 жыл бұрын

    +Ferris Bueller yes, this is part of my last assignment and will make up most of the final

  • @MrTomro
    @MrTomro4 жыл бұрын

    Amazing explanation. Thanks

  • @ersandy4u
    @ersandy4u9 жыл бұрын

    Excellent for someone working closer to the hardware and memory!! Great video. Keep it up

  • @philip-neridzathor8115
    @philip-neridzathor81153 жыл бұрын

    Great explanation.

  • @parasbhanot
    @parasbhanot12 жыл бұрын

    best c++ fundamental videos on youtube

  • @morriskeller
    @morriskeller3 жыл бұрын

    excellent explanation!

  • @aribakodes5453
    @aribakodes54534 жыл бұрын

    Agree, finally understood the concept

  • @vichitt1
    @vichitt111 жыл бұрын

    Thank you for explaining it so well!!!

  • @akhtarabbas5760
    @akhtarabbas57606 жыл бұрын

    outstanding sir.

  • @Yh-gu3cw
    @Yh-gu3cw6 жыл бұрын

    Thank you. I understood everything you said.

  • @treasure2387
    @treasure238711 жыл бұрын

    The stack memory is managed by the program/compiler Dangling pointer = pointer that points to something that no longer exists. you can assign a dangling pointer the value NULL or 0 When a function exits its memory on the stack is deleted.

  • @jenishghale6672
    @jenishghale66728 жыл бұрын

    Very Useful. Thankyou

  • @susie5245
    @susie52455 жыл бұрын

    Wow great video! I completely get it now :) Doing a quick review, haven't touched this material in a while haha

  • @mad8ry
    @mad8ry7 жыл бұрын

    Thank you so much for your effort man! your doing such a great job... I have only one question, is there any way to get your notes for the videos you ?

  • @rosna_773
    @rosna_7736 жыл бұрын

    You are just awesome! thank you so much!

  • @NoobcraftReker
    @NoobcraftReker10 жыл бұрын

    Thank you so much! I finally understand why pointers are useful! I have been facepalming over them ever since I first read about them.

  • @MarijnKonings
    @MarijnKonings11 жыл бұрын

    Thanks, this was a really good explenation.

  • @maxim25o2
    @maxim25o23 жыл бұрын

    Greate explanation.

  • @Ronaldo637_
    @Ronaldo637_11 жыл бұрын

    really nice article about memory

  • @AlphaCentauriVirgo
    @AlphaCentauriVirgo10 жыл бұрын

    Yea nullptr uses in C++ from the book. Very good video very clear. Better than the book.

  • @AlbertoRivas13
    @AlbertoRivas134 жыл бұрын

    This is great man thanks a lot

  • @schifferay
    @schifferay4 жыл бұрын

    really really really helpful! thank you!

  • @hassansoleimani1061
    @hassansoleimani10615 жыл бұрын

    Very very good. Thanks

  • @ricardodelgado9125
    @ricardodelgado91257 жыл бұрын

    thanks friend greetings from Colombia :-)

  • @gbrain12345
    @gbrain1234511 жыл бұрын

    Very good, i like it much !

  • @peksn
    @peksn4 жыл бұрын

    Holly shit you explaint it like no other, literally 15 min and I understand it all now! Thank you!! :D

  • @diverseprogrammer18
    @diverseprogrammer185 жыл бұрын

    OOOOHHH MYYY GAAAADD! Pointer's too simple now that I learned it from u

  • @nelsonmei9527
    @nelsonmei95276 жыл бұрын

    I finally understand this concept

  • @BeansEnjoyer911
    @BeansEnjoyer9112 жыл бұрын

    Great video

  • @thefacelessmen2101
    @thefacelessmen21017 жыл бұрын

    Thanks now it all makes sense.

  • @MyStuff774
    @MyStuff77411 жыл бұрын

    I understand it now, thanks for the video

  • @MrVibriocholerae
    @MrVibriocholerae9 жыл бұрын

    Thank you THANK you THANK YOU!!!

  • @sushilbk2007
    @sushilbk200711 жыл бұрын

    really informative !! thanks !

  • @nick60444
    @nick6044410 жыл бұрын

    I'm actually kinda scared to start practicing on pointers... I don't really understand them to well...much reading up on to do. But the last example you did with the function sparked a question. If you close the program, then what happens to the garbage? Does it get deleted? Cleared?

  • @DKWiseman

    @DKWiseman

    9 жыл бұрын

    When you start your program - OS allocates some memory for it, then your application messes with memory that it is given(storing everything it needs - text, globals, heap, and stack). After it terminates, OS is responsible for cleaning up the mess, so memory will be freed

  • @DKWiseman

    @DKWiseman

    7 жыл бұрын

    1. Theoretically - yes, practically - not really. The address that contains garbage is still occupied, so nothing will be written there by the program or OS automatically unless you use a delete operator to tell that the memory address is now available for use (it doesn't really mean that the value is deleted, it might still be there it just marks the address as available and might or might not be used again). The only way to replace the value guaranteed is to get the address, and to do so you have to manually increment memory addresses and check whats in there, but another problem rises - how would you know what is a garbage value? For example 53453453453 can mean garbage or actual value, it can be an integer that fits entirely in a single memory address or can only be a portion of a large object that continues in the next address. That leads to your next question 2. This one is even more complicated, since it depends on implementation of memory management in OS, in general heap is not guaranteed to be contiguous, however more than often you will see the opposite if you write a program to demonstrate it (the program is tiny and will terminate quick so the whole heap will be within certain range). Also all(?) modern OS use virtual memory technique, meaning that the memory used by a process is different from actual physical addresses where stuff is stored, and on top of all that paging and swapping are used to move memory around. Hope that helps a little bit, also note that I am not a C++ guru and might be wrong on something. Please read more about terms I used in my answer to get a better undestanding

  • @syntaxed2

    @syntaxed2

    6 жыл бұрын

    You dont have to start with pointers, they are not needed at all until well into mid-tier knowledge. When you need to move beyond the memory limit of a stack you will know 100% enough.

  • @benoboro20
    @benoboro2011 жыл бұрын

    thank you ! you are my teacher !!!

  • @lizhu8548
    @lizhu85484 жыл бұрын

    The "garbage", i guess is called "memory leak". Good video!

  • @1qaribullah
    @1qaribullah7 жыл бұрын

    Great explanation. Is it possible to find size of the reserved memory for a specific variable in heap? For stack, it is sizeof() but what would be the case for heap????

  • @davidl.castillo7517
    @davidl.castillo75175 жыл бұрын

    amazing video!!

  • @amritachopra8265
    @amritachopra826511 жыл бұрын

    It was Amazing!! Thanx a lot :)

  • @zcarla3
    @zcarla36 жыл бұрын

    Thanks for the info:-)!!!

  • @dysonmays6842
    @dysonmays68428 жыл бұрын

    liked and subscribed thank you.

  • @anubhavjain700
    @anubhavjain7009 жыл бұрын

    very useful video....carry on

  • @imortalakshay
    @imortalakshay11 жыл бұрын

    you are awesome sir

  • @ImaginaryHuman072889
    @ImaginaryHuman0728897 жыл бұрын

    this video explains alot. however, as a beginning, I still have a few questions.1. Variables stored in the stack DO have variable names and variables stored in the heap DO NOT have variable names, correct?2. What is the advantage/disadvantage of storing variables in the stack vs. in the heap?3. This is probably a stupid question, but why is it necessary to have a stack and a heap? Why can they not be considered one and the same? Then pointers would not be necessary?

  • @T-She-Go
    @T-She-Go3 жыл бұрын

    Hi :) thank you for such a wonderful lesson. Just a quick question: is there a to get *p to point back to memory address 500? Like say for instance we point to 500 (after we allocated space at address 500) and then we point to 700 ((after we allocated space at address 700). Can pointer p point back to 500?