Dynamic Memory Allocation -- C++ Pointers Tutorial [8]

Sdílet
Vložit
  • čas přidán 29. 08. 2024
  • Let's learn about dynamic memory allocation, what it is, how to use it and when to use it. We'll cover the new and delete operators and talk about memory leaks and how to avoid them. In this C++ programming tutorial for beginners, you'll learn through coding examples done using Visual Studio 2022 C++ Community.
    In addition, you'll see how to create a new array and a new int using the new operator and how to free the memory by using the delete operator.
    // Learn More
    Pointers Playlist ⇒ • Playlist
    • Playlist
    C++ Programming for Everyone ⇒ • Playlist
    // Consider supporting this channel in multiple ways
    ko-fi.com/prof...
    paypal.me/hank...
    Bitcoin: 177wfkQwzXiC8o2whQMVpSyuWUs95krKYB
    Dogecoin: DRK2HDp3ZkkbFpvGZnVgMVosnBhPv8r3uP

Komentáře • 49

  • @broozkej8518
    @broozkej8518 Před rokem +28

    I’m a second semester cpp student and you literally cleared up so much information compared to my hours of reviewing and reading online. Thanks bro

  • @treyandrews201
    @treyandrews201 Před rokem +13

    I'm not a student of yours but I am a cs student. Thank you for the video. Your way of explaining it is very easy to understand. I see you have a whole catalog of videos so I'm definitely going to be relying on you to help understand the more difficult concepts

  • @rizzmaster5000
    @rizzmaster5000 Před 3 měsíci +2

    Excellent explanation, I love how meticulous you were when describing all the ins and outs. Great video!

  • @matthewhrndz
    @matthewhrndz Před rokem +2

    By far the best resource for learning about dynamic memory allocation! Using this video to study for my final next week :)

  • @lorddoinkus6349
    @lorddoinkus6349 Před rokem +1

    One of the topics I've had the most trouble with and this explained what a full semester couldn't!

  • @linkofcrafting8929
    @linkofcrafting8929 Před rokem +2

    This is helping me in my second college c++ class!

  • @BigCatSoftware
    @BigCatSoftware Před 8 měsíci

    This was so easy to understand. Thank you! Best memory allocation video on CZcams!

  • @chiyang3749
    @chiyang3749 Před měsícem

    we had to watch a lot of commercial before we could even begin your tutorial

  • @GEAG08
    @GEAG08 Před rokem

    Best explanation ever, would have loved to listen what you said before doing the outro haha, but anyway, thanks for these videos!

  • @Truth_Seeker1202
    @Truth_Seeker1202 Před rokem

    Best video on dynamic memory allocation

  • @mohamedboubakeur4554
    @mohamedboubakeur4554 Před 3 měsíci

    Thanks for teaching , sir 😊

  • @duckie_gamez
    @duckie_gamez Před rokem

    Thank you for making this available! I can't wait for the next lecture.

    • @ProfessorHankStalica
      @ProfessorHankStalica  Před rokem

      You are welcome. I have one more video planned for this series. I'm hoping to have it ready by Friday.

  • @sameaspastmueed
    @sameaspastmueed Před rokem +1

    Hey, hank, I really appreciate you for making this available to everyone l. Was this the last lecture on pointers for now?

    • @ProfessorHankStalica
      @ProfessorHankStalica  Před rokem +1

      Thanks for the kind words! I think I have one more to do in this series for now, and that will cover returning pointers from functions.

    • @liboyan7010
      @liboyan7010 Před rokem

      @@ProfessorHankStalica Pointer to C++ Classes??

    • @ProfessorHankStalica
      @ProfessorHankStalica  Před rokem

      Just like any other type of variable. Assume a class definition named Foo. Then,
      Foo* f = new Foo;

  • @stevenrashid5810
    @stevenrashid5810 Před 3 měsíci +1

    First of all great, amazing video but arent we supposed to reassign the pointer to nullptr...after deleting the memory?

    • @ProfessorHankStalica
      @ProfessorHankStalica  Před 3 měsíci +1

      That's best practice, definitely. There's no syntactical requirement to do so though.

    • @stevenrashid5810
      @stevenrashid5810 Před 3 měsíci

      @@ProfessorHankStalica Thank you for the explanation

  • @mohamednusair2722
    @mohamednusair2722 Před 5 měsíci

    i understand clearly. 😍

  • @user-no9xf5ng4v
    @user-no9xf5ng4v Před měsícem

    Why bald guy is always so fantastic af

  • @qqqqssss510
    @qqqqssss510 Před 3 měsíci

    i think there is a mistake in the video. The memory for n (the pointer itself) is allocated on the stack when the main function is called. This allocation happens at runtime, not at compile time.. during compile time actual physical memory is not allocated to the variables because main is called like a normal function. n is allocated at run time not compile time . same with all other vars in the video.

    • @ProfessorHankStalica
      @ProfessorHankStalica  Před 2 měsíci

      Of course you are correct.
      Our text only talks about compile time vs run time at this level so I stuck with that to make it less confusing for our students by staying consistent with the text.
      A proper assembly programming course will spell it out in detail, which is beyond the scope of this course and video.

  • @syedimadhaqqi4340
    @syedimadhaqqi4340 Před 9 měsíci

    Dear Professor, This is the best video on this topic I have come across. I want to know how can I get the "heap" memory address of the "new int" stored in int *n or int* o. Dereferencing n and o gives garbage output.

    • @ProfessorHankStalica
      @ProfessorHankStalica  Před 8 měsíci

      Not sure I understand the question. You can share a snippet of code of what you are trying to do?

  • @mitchoulina4324
    @mitchoulina4324 Před 6 měsíci +1

    dynamic hair allocation failed 💀 (jk awesome video) u are awesome

  • @dunyacpp
    @dunyacpp Před rokem

    Thank you sir !

  • @ARSIL_AFRIDI
    @ARSIL_AFRIDI Před rokem

    Great sir

  • @its4theer
    @its4theer Před měsícem

    im so frustrated, everyone says its so we can creat arrays whos size is defined by the user.
    so why don't we just do
    {
    int size; cout>size;
    int array[size];
    for(int x:array) cout

    • @ProfessorHankStalica
      @ProfessorHankStalica  Před měsícem

      static memory is allocated on the stack and only remains available while it is in scope. For example, local variable in a function and once the function finishes executing, the memory is gone.
      dynamic memory is allocated on the heap and remains until you explicitly free it using delete. So it will persist because it is not bound to scoping rules as static memory is.
      In other words, if you want to use that array anywhere other than the scope in which it's declared, you can't.

  • @joey6047
    @joey6047 Před rokem

    Thank u Hank :)

    • @ProfessorHankStalica
      @ProfessorHankStalica  Před rokem

      Hi, Joey. I'm really happy you found the video useful.
      And thanks for the PayPal contribution. I really appreciate it!

  • @alexander_kozin9214
    @alexander_kozin9214 Před měsícem

    istn't an integer equals 8 bytes not 4?

    • @ProfessorHankStalica
      @ProfessorHankStalica  Před měsícem

      4 bytes for an int, 2 bytes for a short, 1 byte for a char, and 8 bytes for a long long. Although, technically it can vary based on architecture. Most modern systems will have the above.
      doubles are 8 bytes and floats are 4 bytes.
      see: en.cppreference.com/w/cpp/language/types

  • @azizbeksavkimov7279
    @azizbeksavkimov7279 Před rokem

    Trypophobia (((