Dynamic Memory Allocation in C++
C++ Dynamic Memory Allocation. In this video you will learn the difference between static (stack memory) and dynamic (heap memory/free store). The stack memory is allocated at compile time, which is when your program is converted from c++ into machine code. At this time there are checks for syntax errors and how much stack memory is needed. Variables and function calls are stored on the stack memory. Heap memory on the other hand is allocated at run time, and is much larger than stack memory. Because C++ does not have garbage collection, you will need to free any memory allocated on the heap, otherwise there will be a memory leak! To allocate memory on the heap, use the new keyword, and to deallocate/free memory on the heap, use the delete keyword. After freeing the memory, make sure you avoid dangling pointers by assigning the pointer, nullptr.
If you need to review these topics:
Arrays: https://youtu.be/TjIUYNdbmFk
Pointers: https://youtu.be/FSOnAszRFTk
Pointers to Arrays/ Pointer Arithmetic: https://youtu.be/mlTjAmqtNA8
Null Pointers: https://youtu.be/1QMJQ8dCzJQ
C++ Playlist:
https://www.youtube.com/playlist?list=PLnKe36F30Y4ZDNIOZ51sX25pWKQ1pkpTE
Install C++ with VS Code:
https://youtu.be/DMWD7wfhgNY
Subscribe for more coding tutorials 😄!
Other Videos By Kenny Yip Coding
2024-04-24 | Recursion in C++ |
2024-04-23 | Functions in C++ Practice Problems |
2024-04-22 | C++ Templates and Generics Functions |
2024-04-17 | C++ Const Reference |
2024-04-16 | C++ Function Overloading |
2024-04-15 | C++ Functions Pass by Value or Reference |
2024-04-08 | C++ Functions |
2024-03-27 | Pointers to Pointers C++ |
2024-03-26 | C++ Dynamic Arrays vs Dynamically Allocated Arrays |
2024-03-25 | C++ Dynamic Arrays (Dynamically Allocated Array) |
2024-03-20 | Dynamic Memory Allocation in C++ |
2024-03-19 | C++ Pointers and Arrays |
2024-03-18 | C++ Null Pointers |
2024-03-14 | References in C++ |
2024-03-13 | C++ Const Pointers vs Pointer To Const |
2024-03-12 | Pointers in C++ |
2024-03-11 | Tic Tac Toe Game in C++ |
2024-03-10 | Multidimensional Arrays in C++ (2D arrays) |
2024-02-29 | Rock Paper Scissors Game in C++ |
2024-02-20 | C++ Switch Statement |
2024-02-19 | C++ Break and Continue Statements |