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

Поделиться
HTML-код
  • Опубликовано: 21 янв 2025

Комментарии • 236

  • @jasonitsme
    @jasonitsme 4 года назад +6

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

  • @hassanbentiba4504
    @hassanbentiba4504 5 лет назад +5

    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

  • @irem2719
    @irem2719 Год назад

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

  • @Neothejew
    @Neothejew 7 лет назад +62

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

  • @BrianBurtner
    @BrianBurtner 5 лет назад

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

  • @chachamarwa6047
    @chachamarwa6047 7 лет назад +3

    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

  • @imranabdalla5330
    @imranabdalla5330 7 лет назад +254

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

    • @Guest-gy9vp
      @Guest-gy9vp 5 лет назад +15

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

    • @rajab4187
      @rajab4187 5 лет назад +8

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

    • @EduInquisitive
      @EduInquisitive 4 года назад +16

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

    • @DonSanchezDK
      @DonSanchezDK 4 года назад +20

      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 года назад +2

      @@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 !

  • @harshitakitchen
    @harshitakitchen 5 лет назад +42

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

    • @bdiwy6187
      @bdiwy6187 4 года назад +2

      Nice 👍🙂

    • @sharonjoe7535
      @sharonjoe7535 3 года назад +1

      @Abhinav Rao LMAO

    • @hemiacetal1331
      @hemiacetal1331 3 года назад

      @Abhinav Rao bruh moment

    • @eien7228
      @eien7228 3 года назад

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

    • @abhishektiwari7643
      @abhishektiwari7643 2 года назад +1

      @Harsita's Kitchen do you code while cooking?

  • @cameronsluiter323
    @cameronsluiter323 8 лет назад +37

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

  • @g14f
    @g14f 2 года назад

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

  • @ArchivesMemoriesandWhatnots
    @ArchivesMemoriesandWhatnots 12 лет назад +4

    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!

  • @amiralihosseinzadeh7336
    @amiralihosseinzadeh7336 2 года назад +1

    the best 13 minutes of my life

  • @bodduavinash1
    @bodduavinash1 11 лет назад

    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.

  • @blakewhitlock2307
    @blakewhitlock2307 4 года назад +1

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

  • @ayeyo4081
    @ayeyo4081 4 года назад

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

  • @MyLittleMagneton
    @MyLittleMagneton 9 лет назад +138

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

    • @chcipnifasisto
      @chcipnifasisto 8 лет назад +5

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

    • @zoranjovanov8564
      @zoranjovanov8564 8 лет назад +1

      =0 isjust fine anyway.

    • @MyLittleMagneton
      @MyLittleMagneton 8 лет назад +34

      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 лет назад +3

      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.

  • @War-Cry
    @War-Cry 9 лет назад

    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.

  • @_._lis_._
    @_._lis_._ 3 года назад

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

  • @Kazner0h
    @Kazner0h Год назад

    Those illustrations were SOOOOO helpful. Thanks!

  • @fireflynahian6878
    @fireflynahian6878 5 лет назад +7

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

  • @avanishpatel4936
    @avanishpatel4936 6 лет назад

    One of the best explained topic I ever seen

  • @ASDFG856
    @ASDFG856 9 лет назад +8

    best pointer explanation ever :D

  • @louischvs9395
    @louischvs9395 7 лет назад

    The best video I've ever watched about pointers

  • @AnmolSharma293
    @AnmolSharma293 11 лет назад

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

  • @JO-ct6dl
    @JO-ct6dl 2 года назад

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

  • @hongminwang6747
    @hongminwang6747 9 лет назад

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

  • @muthu6041
    @muthu6041 6 лет назад

    He is so simple in explaining complex concepts. thank you

  • @alive4metal453
    @alive4metal453 7 лет назад

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

  • @fizzanasir572
    @fizzanasir572 4 года назад

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

  • @mukhtarthechosenone7851
    @mukhtarthechosenone7851 4 года назад

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

  • @ersandy4u
    @ersandy4u 10 лет назад

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

  • @parasbhanot
    @parasbhanot 12 лет назад

    best c++ fundamental videos on youtube

  • @TA-vm8es
    @TA-vm8es 11 лет назад

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

  • @alexanderjack8724
    @alexanderjack8724 11 лет назад

    Excellent video. You really capture the big ideas well.

  • @jiroquijano
    @jiroquijano 12 лет назад

    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

  • @treasure2387
    @treasure2387 11 лет назад +2

    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.

  • @BrianVandrian
    @BrianVandrian 12 лет назад

    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

  • @icemotion1925
    @icemotion1925 3 года назад

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

  • @steve018574
    @steve018574 6 лет назад

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

  • @michaelwright8576
    @michaelwright8576 4 года назад

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

  • @foreveralone4813
    @foreveralone4813 8 лет назад

    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

  • @BelowHorizon
    @BelowHorizon 11 лет назад

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

  • @SunSay
    @SunSay 2 года назад +1

    History
    In 1955, Soviet computer scientist Kateryna Yushchenko invented the Address programming language that made possible indirect addressing and addresses of the highest rank - analogous to pointers. This language was widely used on the Soviet Union computers. However, it was unknown outside the Soviet Union and usually Harold Lawson is credited with the invention, in 1964, of the pointer.[2] In 2000, Lawson was presented the Computer Pioneer Award by the IEEE "[f]or inventing the pointer variable and introducing this concept into PL/I, thus providing for the first time, the capability to flexibly treat linked lists in a general-purpose high-level language".[3] His seminal paper on the concepts appeared in the June 1967 issue of CACM entitled: PL/I List Processing. According to the Oxford English Dictionary, the word pointer first appeared in print as a stack pointer in a technical memorandum by the System Development Corporation.
    en.wikipedia.org/wiki/Pointer_(computer_programming)#History

  • @NoobcraftReker
    @NoobcraftReker 10 лет назад +3

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

  • @RockSmithStudio
    @RockSmithStudio 11 лет назад

    You are incredible! Great presentation! Keep it up!

  • @peksn
    @peksn 4 года назад

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

  • @lizhu8548
    @lizhu8548 5 лет назад

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

  • @badis23
    @badis23 7 лет назад

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

  • @anoobis8674
    @anoobis8674 6 лет назад

    Very informative video! Keep it up.

  • @mohammedkashifqamer8665
    @mohammedkashifqamer8665 2 года назад

    Thanks for such an easy explanation

  • @psamtik658
    @psamtik658 8 лет назад +3

    I HAVE A QUESTION!
    this is my code:
    ____________________________________________________________
    int *p = new int(3);
    cout

  • @uruta09
    @uruta09 4 года назад +1

    4:31 That voice crack though haha

  • @AyaNori
    @AyaNori 2 года назад

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

  • @ReelLearning
    @ReelLearning  12 лет назад +2

    I removed a comment by mistake. Apologies to the user that left the nice comment. I had replied "Thanks!" to your comment under my other account and wanted to reply under my ReelLearning account. Deleting my comment, deleted your comment, for some reason.

  • @FriedaNgaharjo
    @FriedaNgaharjo 12 лет назад

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

  • @rogerhunter313
    @rogerhunter313 4 года назад

    This was a great explanation. Thank you!

  • @yapayzeka
    @yapayzeka 5 лет назад

    a legendary explanation. thank you sir.

  • @AlphaCentauriVirgo
    @AlphaCentauriVirgo 11 лет назад

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

  • @rajcodes100
    @rajcodes100 3 года назад

    This was crystal clear .Thanks

  • @oaomybb
    @oaomybb 3 года назад

    very clear clarification. Thanks man

  • @lahiru1k
    @lahiru1k Год назад

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

  • @mhmmmmmmm
    @mhmmmmmmm 7 лет назад

    At 4:36 I am confused you said that you were dereferencing the pointer p when you were changing its value. Does the asterisk mean "dereference", I thought to "dereferencing" was only when you were getting the value of whats being pointed at, not reassigning its value. I'm not trying to be difficult I just don't know.

  • @lambda8763
    @lambda8763 8 лет назад +1

    So if int* p = new int; creates a pointer on the stack and space for an int on the heap, does the following allocate an int on the stack instead?
    int x = 5;
    int* p = &x;
    Or is x still created on the heap?

    • @lockbert99
      @lockbert99 8 лет назад

      +Lambda x is on the stack in your example, assuming it is being declared inside a function. If declared globally (outside a function) it would be in what the video calls the "static" area.
      Stack memory is only guaranteed to be valid for the life of the function so pointers to stack memory should not be returned from the function.

    • @lambda8763
      @lambda8763 8 лет назад

      +lockbert99 Cheers, thanks for clarifying that for me.

  • @FluffyNinjaUnicorn3Gaming
    @FluffyNinjaUnicorn3Gaming 4 года назад

    Very informative video! Thank you so much!

  • @1415gatewayable
    @1415gatewayable 11 лет назад +2

    Rockstar should learn this...

  • @nick60444
    @nick60444 10 лет назад +9

    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 10 лет назад +10

      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 лет назад +6

      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.

  • @marianaql
    @marianaql 7 лет назад

    I loved your explanation. Thank you!

  • @diverseprogrammer18
    @diverseprogrammer18 5 лет назад +1

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

  • @henrikaskancys4956
    @henrikaskancys4956 5 лет назад

    Short and informative, thank you!

  • @amirsabbaghi209
    @amirsabbaghi209 7 лет назад

    I didn't get the part that was talking about creating garbage . there were 2 ways of creating garbage right ? One forgetting to delete . The other was defining a local pointer and when the program goes out of that loop or function then the memory will be occupied on the heap yet . What should we do to avoid this now ?? Ans pls 🙏🙏🙏

    • @jacquesgilbert2774
      @jacquesgilbert2774 7 лет назад

      In both cases, an area of memory became garbage because the pointer that was used to access it was no longer available. In the first case, the pointer was used to point to a new variable, and the initial address was lost (overwritten); in the second case, the pointer was allocated on the stack as part of the function then discarded when the function terminated. The result was the same in that the memory area allocated to the variable declared with the "new" statement was no longer accessible. In both cases, the solution is to free that memory with a "delete" statement while the pointer is still available: before reassigning it, or before the function terminates.

  • @ImaginaryHuman072889
    @ImaginaryHuman072889 7 лет назад

    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?

  • @raihanrony1810
    @raihanrony1810 4 года назад

    so can I write
    int *p = new int;
    *p=5;
    p = NULL;
    and skip ( delete p ; ) line like that ?

  • @1qaribullah
    @1qaribullah 8 лет назад

    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????

  • @purushothamreddy4453
    @purushothamreddy4453 8 лет назад +1

    Awesome explanation

  • @yayaskurt
    @yayaskurt 8 лет назад

    What happens if u store p on a vector, then creates a new p and stores it in the vector, can u access both?

  • @Quancept
    @Quancept 5 лет назад

    Thanks man, really good explanation!

  • @susie5245
    @susie5245 6 лет назад

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

  • @devenmhatre727
    @devenmhatre727 6 лет назад

    my cpp dll is creating a dynamic array, and the call from exe is getting back the pointer pointing to the dynamic array. The problem is that the returning pointer is null every time. Even though the pointer is pointing to a array in the dll.
    How can we solve this problem.

  • @philip-neridzathor8115
    @philip-neridzathor8115 4 года назад

    Great explanation.

  • @2glizzyy
    @2glizzyy 6 лет назад +19

    4:31 boi hit puberty for a sec

  • @morriskeller
    @morriskeller 4 года назад

    excellent explanation!

  • @krupt5995
    @krupt5995 6 лет назад

    So if heap memory cant cause only program polution by garbage and other problems why are we using it?

  • @naython13
    @naython13 5 лет назад

    Newbie here, why would you use a variable in the heap and not the stack?

  • @MrTomro
    @MrTomro 5 лет назад

    Amazing explanation. Thanks

  • @skrame01
    @skrame01 4 года назад

    What is the point of making a variable 'on the heap' as opposed to in the function (stack).

    • @darkengine5931
      @darkengine5931 4 года назад

      Variables don't actually use any memory. They're neither allocated on heap nor stack.

  • @Ronaldo637_
    @Ronaldo637_ 12 лет назад

    really nice article about memory

  • @aribakodes5453
    @aribakodes5453 4 года назад

    Agree, finally understood the concept

  • @Stomachbuzz
    @Stomachbuzz 3 года назад

    Why does 'delete' not actually delete p?
    Like the entire variable?
    Otherwise, isn't the memory for that int still wasted?

  • @CellarPhantom
    @CellarPhantom 11 лет назад

    Soo, if I make normal variables I do not need to delete them right? And I think it is hard to understand when I should make variables on the heap and when not to. It's easier to always do 'normal' variables.

  • @AlbertoRivas13
    @AlbertoRivas13 5 лет назад

    This is great man thanks a lot

  • @francisDr4ke
    @francisDr4ke 5 лет назад

    very great explaination. thank you

  • @sreenathak6355
    @sreenathak6355 3 года назад

    6:28 its also called memory leakage right

  • @Dennisbot
    @Dennisbot 11 лет назад

    Just a doubt, in the function x() when you don't use delete to deallocate memory in spite of being empty (because you just did new int, instead of new int(10)), it's not the same as if it wasn't allocated and that way stay free?, or exist some mechanism that prevents use that section of memory due to the lack of a delete statement for the same pointer that was declared with the new operator?, for the rest,it was completely pleasant to watch, thank you for share knowledge!, greetings from Perú.

  • @akhtarabbas5760
    @akhtarabbas5760 7 лет назад

    outstanding sir.

  • @BryanChance
    @BryanChance 2 года назад

    Would it be more appropriate to call it a “memory leak” than “garbage”?

  • @warzonemoments3970
    @warzonemoments3970 4 года назад

    I don't get why we need to do this. Why not just work with variables?

  • @vichitt1
    @vichitt1 12 лет назад

    Thank you for explaining it so well!!!

  • @BrewmasterDonegan
    @BrewmasterDonegan 5 лет назад

    I LOVE YOU! YOU SAVED MY LIFE !

  • @nelsonmei9527
    @nelsonmei9527 6 лет назад

    I finally understand this concept

  • @MrRobertantoniopgore
    @MrRobertantoniopgore 10 лет назад

    I ran an example of what you explained at 09:13 and the address of the pointer is the same. Only the value changed. Did I do something wrong or maybe the explanation was wrong? Here's my code:
    int *p = new int;
    *p = 3;
    cout

    • @user-lz4ko6tb6h
      @user-lz4ko6tb6h 10 лет назад

      Your code is fine. Your computer probably picked the same memory location because nothing else used it in the time that your code ran. Memory locations are not picked at random.