title
Stack vs Heap Memory in C++

description
Patreon ► https://patreon.com/thecherno Twitter ► https://twitter.com/thecherno Instagram ► https://instagram.com/thecherno Discord ► https://thecherno.com/discord Series Playlist ► https://thecherno.com/cpp malloc links: http://gee.cs.oswego.edu/dl/html/malloc.html https://people.freebsd.org/~jasone/jemalloc/bsdcan2006/jemalloc.pdf Thank you to the following Patreon supporters: - Dominic Pace - Kevin Gregory Agwaze - Sébastien Bervoets - Tobias Humig - Peter Siegmund - Kerem Demirer Gear I use: ----------------- BEST laptop for programming! ► http://geni.us/pakTES My FAVOURITE keyboard for programming! ► http://geni.us/zNhB FAVOURITE monitors for programming! ► http://geni.us/Ig6KBq MAIN Camera ► http://geni.us/t6xyDRO MAIN Lens ► http://geni.us/xGoDWT Second Camera ► http://geni.us/CYUQ Microphone ► http://geni.us/wqO6g7K

detail
{'title': 'Stack vs Heap Memory in C++', 'heatmap': [{'end': 59.561, 'start': 41.587, 'weight': 0.763}, {'end': 305.581, 'start': 264.176, 'weight': 0.739}, {'end': 645.48, 'start': 627.606, 'weight': 0.705}], 'summary': "Explores memory allocation in c++, comparing stack and heap memory, emphasizing efficient memory management and the use of 'new' keyword for heap allocation, demonstrating debugging in visual studio, and discussing the advantages of stack memory deallocation and its performance benefits in game engines.", 'chapters': [{'end': 284.229, 'segs': [{'end': 80.292, 'src': 'heatmap', 'start': 41.587, 'weight': 0, 'content': [{'end': 47.632, 'text': 'it will load the entire program into memory as well as allocate a whole bunch of physical RAM so that our actual application can run.', 'start': 41.587, 'duration': 6.045}, {'end': 52.976, 'text': 'Now, the stack and the heap are two areas that we actually have in our RAM.', 'start': 48.212, 'duration': 4.764}, {'end': 59.561, 'text': 'The stack is typically an area of memory that has a predefined size, usually around two megabytes or so.', 'start': 53.497, 'duration': 6.064}, {'end': 64.105, 'text': 'And the heap is an area that is also kind of predefined to a default value.', 'start': 60.002, 'duration': 4.103}, {'end': 67.567, 'text': 'However, it can grow and change as our application goes on.', 'start': 64.144, 'duration': 3.423}, {'end': 74.549, 'text': "Now, it's important to note that the actual location, the physical location of these two areas of memory is ultimately the same.", 'start': 68.007, 'duration': 6.542}, {'end': 75.37, 'text': "It's in our RAM.", 'start': 74.65, 'duration': 0.72}, {'end': 80.292, 'text': "A lot of people tend to think that the stack might be something that's stored in the CPU cache or something like that.", 'start': 75.93, 'duration': 4.362}], 'summary': 'Program loads into memory, stack and heap in ram, stack size around 2mb.', 'duration': 38.705, 'max_score': 41.587, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/wJ1L2nSIV1s/pics/wJ1L2nSIV1s41587.jpg'}, {'end': 150.895, 'src': 'embed', 'start': 124.911, 'weight': 1, 'content': [{'end': 129.435, 'text': 'We can ask C++ to give us some memory from either the stack or the heap,', 'start': 124.911, 'duration': 4.524}, {'end': 133.518, 'text': 'and it will give us a block of memory of our requested size if everything goes well.', 'start': 129.435, 'duration': 4.083}, {'end': 136.4, 'text': 'The difference is how it allocates that memory for us.', 'start': 133.698, 'duration': 2.702}, {'end': 139.443, 'text': 'So suppose, for an example, that we want to be able to store an integer.', 'start': 136.5, 'duration': 2.943}, {'end': 142.706, 'text': 'an integer on most platforms is four bytes and int right.', 'start': 139.443, 'duration': 3.263}, {'end': 146.73, 'text': 'so how do we find a contiguous block of four bytes of memory?', 'start': 142.706, 'duration': 4.024}, {'end': 148.312, 'text': 'contiguous just means in a row, right?', 'start': 146.73, 'duration': 1.582}, {'end': 150.895, 'text': 'how do we find that block of four bytes of memory?', 'start': 148.312, 'duration': 2.583}], 'summary': 'C++ allocates memory from stack/heap, e.g., 4 bytes for an integer.', 'duration': 25.984, 'max_score': 124.911, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/wJ1L2nSIV1s/pics/wJ1L2nSIV1s124911.jpg'}, {'end': 219.751, 'src': 'embed', 'start': 192.332, 'weight': 3, 'content': [{'end': 198.296, 'text': 'So you can notice that it is two lines of code, but more importantly, we actually use the new keyword here to allocate on the heap.', 'start': 192.332, 'duration': 5.964}, {'end': 200.578, 'text': "That's what kind of distinguishes these two allocations.", 'start': 198.316, 'duration': 2.262}, {'end': 203.22, 'text': 'That is a stack allocation, and this is a heap allocation.', 'start': 200.918, 'duration': 2.302}, {'end': 205.301, 'text': "With arrays, it's kind of similar.", 'start': 203.84, 'duration': 1.461}, {'end': 209.704, 'text': 'We have int array and then five, for example, for five elements in the array.', 'start': 205.461, 'duration': 4.243}, {'end': 211.265, 'text': 'And on the heap, this would look like this.', 'start': 209.904, 'duration': 1.361}, {'end': 215.108, 'text': 'Int h array for heap array equals new int five, like that.', 'start': 211.445, 'duration': 3.663}, {'end': 219.751, 'text': "So again, the major difference being we're using the new keyword here to actually allocate that memory.", 'start': 215.388, 'duration': 4.363}], 'summary': 'Comparison of stack and heap allocations using new keyword for memory allocation.', 'duration': 27.419, 'max_score': 192.332, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/wJ1L2nSIV1s/pics/wJ1L2nSIV1s192332.jpg'}], 'start': 0.089, 'title': 'Memory allocation in c++', 'summary': "Discusses the concepts of stack and heap memory in c++, default sizes, physical location within ram, allocation of memory for storing data, difference between stack and heap memory, and the use of the 'new' keyword for heap allocation, with a focus on storing an integer of four bytes.", 'chapters': [{'end': 91.402, 'start': 0.089, 'title': 'Understanding c++ memory: stack and heap', 'summary': 'Discusses the concepts of stack and heap memory in c++, highlighting their functionalities, default sizes, and physical location within ram.', 'duration': 91.313, 'highlights': ['The stack and the heap are two key areas of memory in C++, with the stack having a predefined size of around two megabytes, while the heap can grow and change as the application progresses.', 'When the program starts, it gets divided into various memory areas, with the stack and the heap being two crucial ones, both physically located in the RAM.', 'The operating system loads the entire program into memory and allocates physical RAM for the application to run, with the stack and heap being part of the RAM.']}, {'end': 160.525, 'start': 91.402, 'title': 'Memory allocation in c++', 'summary': 'Discusses the allocation of memory in c++ for storing data, explaining the difference between stack and heap memory and their allocation process, primarily focusing on the example of storing an integer of four bytes.', 'duration': 69.123, 'highlights': ['The stack and the heap are two different areas of memory used for storing data in a program.', 'Memory in a program is used for storing data such as local variables or data read from a file for processing.', 'C++ can allocate memory from either the stack or the heap, providing a block of memory of the requested size.', 'The difference lies in how the stack and the heap allocate the memory, particularly in terms of finding a contiguous block of memory, which is essential for memory allocation.', 'An integer on most platforms is four bytes, and the process of finding a contiguous block of four bytes of memory differs between the stack and the heap.']}, {'end': 284.229, 'start': 160.945, 'title': 'Stack vs heap allocation in c++', 'summary': "Compares stack and heap allocation in c++ by demonstrating the allocation of integer, array, and object data types, highlighting the differences and the use of the 'new' keyword for heap allocation.", 'duration': 123.284, 'highlights': ["Demonstrates the allocation of integer, array, and object data types on both stack and heap, emphasizing the differences and the use of the 'new' keyword for heap allocation. Allocation of integer, array, and object data types; Comparison of stack and heap allocation; Use of 'new' keyword for heap allocation", "Explains the distinction between stack and heap allocation, showcasing the use of 'new' keyword for heap allocation and the differences in the number of lines required for each type of allocation. Use of 'new' keyword for heap allocation; Comparison of lines of code for stack and heap allocation", "Illustrates the allocation of integer and array data types on both stack and heap, highlighting the use of 'new' keyword for heap allocation and the differences in syntax. Allocation of integer and array data types; Use of 'new' keyword for heap allocation; Syntax differences"]}], 'duration': 284.14, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/wJ1L2nSIV1s/pics/wJ1L2nSIV1s89.jpg', 'highlights': ['The stack and the heap are two key areas of memory in C++, with the stack having a predefined size of around two megabytes, while the heap can grow and change as the application progresses.', 'C++ can allocate memory from either the stack or the heap, providing a block of memory of the requested size.', 'An integer on most platforms is four bytes, and the process of finding a contiguous block of four bytes of memory differs between the stack and the heap.', "Demonstrates the allocation of integer, array, and object data types on both stack and heap, emphasizing the differences and the use of the 'new' keyword for heap allocation.", "Explains the distinction between stack and heap allocation, showcasing the use of 'new' keyword for heap allocation and the differences in the number of lines required for each type of allocation.", "Illustrates the allocation of integer and array data types on both stack and heap, highlighting the use of 'new' keyword for heap allocation and the differences in syntax."]}, {'end': 538.73, 'segs': [{'end': 320.819, 'src': 'embed', 'start': 284.489, 'weight': 0, 'content': [{'end': 285.27, 'text': 'All right, cool.', 'start': 284.489, 'duration': 0.781}, {'end': 287.851, 'text': "So now let's put a breakpoint over here and hit F5.", 'start': 285.37, 'duration': 2.481}, {'end': 290.853, 'text': "So the first thing I'm going to do is actually go to the memory address of value.", 'start': 288.031, 'duration': 2.822}, {'end': 298.337, 'text': 'So if I type in ampersand value in my memory view over here, you can access the memory view by going to debug, windows, memory, and then memory one.', 'start': 290.913, 'duration': 7.424}, {'end': 301.259, 'text': 'You can see that it takes me to the memory address of that variable.', 'start': 298.657, 'duration': 2.602}, {'end': 305.581, 'text': "Now CCCCC in debug mode means that we just haven't actually initialized that value yet.", 'start': 301.299, 'duration': 4.282}, {'end': 308.683, 'text': 'So if I hit F10, you can see that we change to five.', 'start': 305.902, 'duration': 2.781}, {'end': 311.105, 'text': 'So now we have that value in our actual memory.', 'start': 308.743, 'duration': 2.362}, {'end': 311.785, 'text': 'Pretty simple.', 'start': 311.245, 'duration': 0.54}, {'end': 312.625, 'text': "Now let's hit F10.", 'start': 312.045, 'duration': 0.58}, {'end': 315.975, 'text': 'and see where this array actually is.', 'start': 314.113, 'duration': 1.862}, {'end': 317.356, 'text': "So I'm just going to type in array.", 'start': 315.995, 'duration': 1.361}, {'end': 319.618, 'text': "It already is a pointer because it's an array and we'll see where we are.", 'start': 317.376, 'duration': 2.242}, {'end': 320.819, 'text': 'Okay, pretty cool.', 'start': 319.978, 'duration': 0.841}], 'summary': 'Debugging code to access memory address and initialize value to 5.', 'duration': 36.33, 'max_score': 284.489, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/wJ1L2nSIV1s/pics/wJ1L2nSIV1s284489.jpg'}, {'end': 418.495, 'src': 'embed', 'start': 389.885, 'weight': 4, 'content': [{'end': 394.866, 'text': 'that is the pointer of like, the top of the stack basically just moves, moves that amount of bytes.', 'start': 389.885, 'duration': 4.981}, {'end': 399.307, 'text': "So if I want to allocate an integer, that's four bytes, we move the stack pointer four bytes.", 'start': 394.886, 'duration': 4.421}, {'end': 399.967, 'text': "That's it.", 'start': 399.587, 'duration': 0.38}, {'end': 407.449, 'text': "If I want to allocate an array like we have here, five integers, that's four times five or 20, 20 bytes, the stack pointer moves 20 bytes.", 'start': 400.047, 'duration': 7.402}, {'end': 409.789, 'text': 'And finally, for our vector three, we have three floats.', 'start': 407.489, 'duration': 2.3}, {'end': 411.75, 'text': 'Each float is four bytes, so 12 bytes.', 'start': 410.069, 'duration': 1.681}, {'end': 413.23, 'text': 'We just move the stack pointer.', 'start': 412.11, 'duration': 1.12}, {'end': 414.01, 'text': 'That is it.', 'start': 413.57, 'duration': 0.44}, {'end': 418.495, 'text': 'The memory is literally stored on top of each other like a stack.', 'start': 414.991, 'duration': 3.504}], 'summary': 'Stack pointer moves to allocate memory for integers and floats.', 'duration': 28.61, 'max_score': 389.885, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/wJ1L2nSIV1s/pics/wJ1L2nSIV1s389885.jpg'}, {'end': 453.871, 'src': 'embed', 'start': 426.242, 'weight': 3, 'content': [{'end': 429.265, 'text': 'So int value is actually stored at a higher memory address.', 'start': 426.242, 'duration': 3.023}, {'end': 434.93, 'text': 'And then we kind of store the array next to it, but kind of backwards at a lower memory address value.', 'start': 429.285, 'duration': 5.645}, {'end': 437.246, 'text': 'because it kind of grows backwards.', 'start': 436.186, 'duration': 1.06}, {'end': 444.928, 'text': 'But the idea of a stack is we literally just stack things on top of each other, which is why a stack allocation is extremely fast.', 'start': 437.626, 'duration': 7.302}, {'end': 447.989, 'text': "It's literally like one CPU instruction.", 'start': 445.488, 'duration': 2.501}, {'end': 453.871, 'text': 'All we do is we move the stack pointer and then we return the address of that stack pointer.', 'start': 448.449, 'duration': 5.422}], 'summary': 'Stack allocation is extremely fast, with one cpu instruction to move the stack pointer and return its address.', 'duration': 27.629, 'max_score': 426.242, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/wJ1L2nSIV1s/pics/wJ1L2nSIV1s426242.jpg'}, {'end': 538.73, 'src': 'embed', 'start': 504.834, 'weight': 5, 'content': [{'end': 507.115, 'text': "So there's not really much point in showing you this memory view anyway.", 'start': 504.834, 'duration': 2.281}, {'end': 510.015, 'text': 'But what I do wanna mention is a few things about this heap allocation.', 'start': 507.415, 'duration': 2.6}, {'end': 512.895, 'text': 'First of all, yes, I am using the new keyword here.', 'start': 510.175, 'duration': 2.72}, {'end': 519.438, 'text': "However, if you were using smart pointers and you were using make unique or make shared, it's exactly the same thing.", 'start': 513.035, 'duration': 6.403}, {'end': 520.921, 'text': 'It will call new for you.', 'start': 519.639, 'duration': 1.282}, {'end': 525.628, 'text': 'And then the other really important thing, of course, is that you actually have to delete memory that you allocate using new.', 'start': 521.361, 'duration': 4.267}, {'end': 528.713, 'text': 'Smart pointers will do that for you, but since we have used new here,', 'start': 525.949, 'duration': 2.764}, {'end': 533.821, 'text': 'we actually have to call delete H value delete H array with the array delete operator here.', 'start': 528.713, 'duration': 5.108}, {'end': 536.45, 'text': 'and delete H vector as well.', 'start': 534.989, 'duration': 1.461}, {'end': 538.73, 'text': 'So we do actually have to manually free our memory.', 'start': 536.63, 'duration': 2.1}], 'summary': 'Heap allocation using new requires manual memory deallocation with delete operator.', 'duration': 33.896, 'max_score': 504.834, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/wJ1L2nSIV1s/pics/wJ1L2nSIV1s504834.jpg'}], 'start': 284.489, 'title': 'Debugging memory and arrays', 'summary': "Demonstrates debugging in visual studio, showcasing the process of accessing memory addresses, initializing variables, and observing the array's contents, and explains the concept of stack and heap memory allocation in c++, emphasizing the efficiency and manual memory management required for heap allocation.", 'chapters': [{'end': 355.263, 'start': 284.489, 'title': 'Debugging memory and arrays', 'summary': "Demonstrates debugging in visual studio, showcasing the process of accessing memory addresses, initializing variables, and observing the array's contents.", 'duration': 70.774, 'highlights': ['The chapter explains accessing memory addresses in Visual Studio by using the memory view, accessed through debug, windows, memory, and then memory one. This provides a visual representation of the memory address of a variable.', 'It demonstrates the process of initializing a variable in debug mode, with the example of changing an uninitialized value to five using the F10 command, showcasing the practical application of debugging.', 'The chapter showcases the observation of an array in memory by using the F10 command, providing a practical demonstration of visualizing the array and its consecutive elements in the memory.', 'It emphasizes the proximity of the memory address of a value and the array in memory, highlighting the practical understanding of memory allocation and the arrangement of variables in memory during debugging.']}, {'end': 538.73, 'start': 355.263, 'title': 'Memory allocation in c++', 'summary': 'Explains the concept of stack and heap memory allocation in c++, demonstrating how variables are stored closely together in stack allocation, while heap allocation results in variables scattered across different memory addresses, with emphasis on the efficiency and manual memory management required for heap allocation.', 'duration': 183.467, 'highlights': ['The stack allocation in C++ allows for variables to be stored closely together in memory due to the stack pointer moving a specific amount of bytes for each variable, resulting in extremely fast allocation and one CPU instruction, as demonstrated by the allocation of integers and arrays.', 'The concept of stack allocation in C++ is exemplified by the demonstration of how the stack pointer moves a specific amount of bytes for each variable, with the allocation of integers, arrays, and vectors being highlighted as examples of the memory being stored on top of each other like a stack, resulting in efficient and fast allocation.', 'The heap allocation in C++ results in variables being stored at different memory addresses, as exemplified by the scattered memory addresses for variables like H value and H array, with emphasis on the necessity of manual memory management and the requirement to delete memory allocated using the new keyword in C++.', 'The process of heap allocation in C++ is explained, emphasizing the need for manual memory management and the requirement to delete memory allocated using the new keyword, with a mention of the equivalency of smart pointers like make unique or make shared to using new, and the necessity of manually freeing memory allocated using the delete operator.']}], 'duration': 254.241, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/wJ1L2nSIV1s/pics/wJ1L2nSIV1s284489.jpg', 'highlights': ['The chapter explains accessing memory addresses in Visual Studio using the memory view, providing a visual representation of the memory address of a variable.', 'The chapter demonstrates the process of initializing a variable in debug mode, showcasing the practical application of debugging.', 'The chapter showcases the observation of an array in memory by using the F10 command, providing a practical demonstration of visualizing the array and its consecutive elements in the memory.', 'The stack allocation in C++ allows for variables to be stored closely together in memory due to the stack pointer moving a specific amount of bytes for each variable, resulting in extremely fast allocation and one CPU instruction.', 'The concept of stack allocation in C++ is exemplified by the demonstration of how the stack pointer moves a specific amount of bytes for each variable, resulting in efficient and fast allocation.', 'The heap allocation in C++ results in variables being stored at different memory addresses, with emphasis on the necessity of manual memory management and the requirement to delete memory allocated using the new keyword in C++.', 'The process of heap allocation in C++ is explained, emphasizing the need for manual memory management and the requirement to delete memory allocated using the new keyword.']}, {'end': 828.312, 'segs': [{'end': 567.082, 'src': 'embed', 'start': 538.871, 'weight': 0, 'content': [{'end': 545.293, 'text': "Whereas with the stack as well, what happens is, once this scope in which you've allocated that stack memory actually ends,", 'start': 538.871, 'duration': 6.422}, {'end': 549.395, 'text': "all of the memory that you've allocated in that stack just gets popped off, it just gets freed.", 'start': 545.293, 'duration': 4.102}, {'end': 555.317, 'text': 'So if we were to do something like wrap this in a scope, it can be any scope, it can be this function scope of this main function,', 'start': 549.755, 'duration': 5.562}, {'end': 560.619, 'text': 'or it can just be an empty scope like this, or like a for loop, a while loop, an if statement, whatever, any kind of scope.', 'start': 555.317, 'duration': 5.302}, {'end': 567.082, 'text': 'When this scope comes to an end, everything that was allocated on the stack inside that scope just gets popped off.', 'start': 561.159, 'duration': 5.923}], 'summary': 'Stack memory is automatically deallocated when the scope ends, freeing up allocated memory.', 'duration': 28.211, 'max_score': 538.871, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/wJ1L2nSIV1s/pics/wJ1L2nSIV1s538871.jpg'}, {'end': 612.455, 'src': 'embed', 'start': 586.75, 'weight': 1, 'content': [{'end': 591.731, 'text': "It's just that, instead of moving our stack pointer backwards and returning that address, we just pop everything off the stack.", 'start': 586.75, 'duration': 4.981}, {'end': 594.612, 'text': 'so our stack pointer goes back up to where it was before the scope began.', 'start': 591.731, 'duration': 2.881}, {'end': 597.492, 'text': "One CPU instruction, it's basically free.", 'start': 595.732, 'duration': 1.76}, {'end': 601.693, 'text': 'Whereas with delete, that obviously has to actually free everything.', 'start': 597.792, 'duration': 3.901}, {'end': 604.674, 'text': "Now let's talk a little bit about what the new keyword actually does.", 'start': 602.033, 'duration': 2.641}, {'end': 606.614, 'text': 'Now I do have a video on this, definitely check that out.', 'start': 604.714, 'duration': 1.9}, {'end': 612.455, 'text': "If you're more interested in this kind of object lifetime stuff as well, I have made a video about that as well.", 'start': 607.074, 'duration': 5.381}], 'summary': 'Using pop instead of delete saves cpu instructions. the new keyword explained.', 'duration': 25.705, 'max_score': 586.75, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/wJ1L2nSIV1s/pics/wJ1L2nSIV1s586750.jpg'}, {'end': 651.325, 'src': 'heatmap', 'start': 621.241, 'weight': 4, 'content': [{'end': 622.862, 'text': 'Definitely recommend checking that out as well.', 'start': 621.241, 'duration': 1.621}, {'end': 627.406, 'text': "In fact, I'll probably just leave a list of videos over there in the top right corner that you can just check out.", 'start': 622.902, 'duration': 4.504}, {'end': 632.65, 'text': "Anyway, let's just talk a little bit about how the heap actually works and what new and delete do.", 'start': 627.606, 'duration': 5.044}, {'end': 637.414, 'text': 'So the new keyword will really just call malloc, a function called malloc or memory allocate.', 'start': 632.79, 'duration': 4.624}, {'end': 643.059, 'text': 'And what that will do is, in turn, usually call the underlying operating system, like platform, specific function,', 'start': 637.814, 'duration': 5.245}, {'end': 645.48, 'text': 'and that will allocate memory for you on the heap.', 'start': 643.059, 'duration': 2.421}, {'end': 651.325, 'text': 'And the way that it does, that is, when you start your application, you get a certain amount of physical RAM kind of allocated to you,', 'start': 645.541, 'duration': 5.784}], 'summary': 'Explanation of how heap works and new/delete functions, using malloc to allocate memory on the heap.', 'duration': 30.084, 'max_score': 621.241, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/wJ1L2nSIV1s/pics/wJ1L2nSIV1s621241.jpg'}, {'end': 744.277, 'src': 'embed', 'start': 705.241, 'weight': 2, 'content': [{'end': 710.202, 'text': "and to make things even worse, if you've asked for more memory than is actually in that free list,", 'start': 705.241, 'duration': 4.961}, {'end': 715.323, 'text': 'in that initial kind of allocation that your operating system has given you, then your application, your program,', 'start': 710.202, 'duration': 5.121}, {'end': 721.725, 'text': 'has to actually ask your operating system hey, i need some more memory please, and that is very expensive.', 'start': 715.323, 'duration': 6.402}, {'end': 724.786, 'text': "so there's that potential cost, which is huge.", 'start': 721.725, 'duration': 3.061}, {'end': 731.77, 'text': "Really, the point that I'm trying to make here is that allocating memory on the heap is a whole thing,", 'start': 725.066, 'duration': 6.704}, {'end': 735.612, 'text': 'whereas allocating memory on the stack is like one CPU instruction.', 'start': 731.77, 'duration': 3.842}, {'end': 740.114, 'text': 'That is all I want you to take away from this video, really.', 'start': 736.392, 'duration': 3.722}, {'end': 744.277, 'text': 'The fact that the differences between these two are primarily the allocation.', 'start': 740.515, 'duration': 3.762}], 'summary': 'Heap memory allocation is expensive, while stack allocation is efficient.', 'duration': 39.036, 'max_score': 705.241, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/wJ1L2nSIV1s/pics/wJ1L2nSIV1s705241.jpg'}], 'start': 538.871, 'title': 'Memory management in stack and heap', 'summary': 'Details the difference between stack and heap memory allocation, highlighting the efficiency advantage of stack memory deallocation with one cpu instruction, and contrasts the process of heap memory allocation with the speed and efficiency of stack memory.', 'chapters': [{'end': 604.674, 'start': 538.871, 'title': 'Memory management in stack and heap', 'summary': 'Explains the difference between stack and heap memory allocation, emphasizing that stack memory gets freed upon scope exit, while heap memory requires explicit deletion, and mentions the efficiency advantage of stack memory deallocation with one cpu instruction.', 'duration': 65.803, 'highlights': ['Stack memory allocated in a scope gets freed upon scope exit, making it efficient and cost-free compared to heap memory.', "Heap memory deallocation with 'delete' requires explicit freeing, unlike stack memory which gets popped off upon scope end.", 'Stack memory deallocation involves moving the stack pointer back to its position before the scope began, requiring only one CPU instruction for deallocation.']}, {'end': 828.312, 'start': 604.714, 'title': 'Heap vs stack memory allocation', 'summary': 'Explains the process of heap memory allocation by the new keyword, the functioning of malloc, the cost of requesting additional memory from the operating system, and contrasts it with the efficiency of stack memory allocation in terms of speed and cache misses, emphasizing that stack memory allocation is faster and more efficient.', 'duration': 223.598, 'highlights': ['The new keyword calls malloc to allocate memory on the heap, which involves maintaining a free list and recording the size of the allocation. The new keyword calls malloc to allocate memory on the heap, which involves maintaining a free list and recording the size of the allocation.', 'Allocating memory on the heap is a heavy process, and requesting additional memory from the operating system is very expensive. Allocating memory on the heap is a heavy process, and requesting additional memory from the operating system is very expensive.', 'The difference in memory allocation speed between the stack and the heap is emphasized, with stack allocation being just one CPU instruction compared to the complexity of heap allocation. The difference in memory allocation speed between the stack and the heap is emphasized, with stack allocation being just one CPU instruction compared to the complexity of heap allocation.']}], 'duration': 289.441, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/wJ1L2nSIV1s/pics/wJ1L2nSIV1s538871.jpg', 'highlights': ['Stack memory allocated in a scope gets freed upon scope exit, making it efficient and cost-free compared to heap memory.', 'Stack memory deallocation involves moving the stack pointer back to its position before the scope began, requiring only one CPU instruction for deallocation.', 'The difference in memory allocation speed between the stack and the heap is emphasized, with stack allocation being just one CPU instruction compared to the complexity of heap allocation.', "Heap memory deallocation with 'delete' requires explicit freeing, unlike stack memory which gets popped off upon scope end.", 'The new keyword calls malloc to allocate memory on the heap, which involves maintaining a free list and recording the size of the allocation.', 'Allocating memory on the heap is a heavy process, and requesting additional memory from the operating system is very expensive.']}, {'end': 1153.146, 'segs': [{'end': 877.122, 'src': 'embed', 'start': 828.312, 'weight': 0, 'content': [{'end': 832.455, 'text': "let's take a look at the generated assembly behind what we've just written and we can see what it actually does.", 'start': 828.312, 'duration': 4.143}, {'end': 836.338, 'text': "i'm just going to compile this code by hitting control f7 in my properties for my project.", 'start': 832.455, 'duration': 3.883}, {'end': 839.36, 'text': "i've also just got under cc plus output files.", 'start': 836.338, 'duration': 3.022}, {'end': 842.682, 'text': "i've got assembler output to assembly with source code so that we can look at it.", 'start': 839.36, 'duration': 3.322}, {'end': 844.023, 'text': 'and here we have the assembly.', 'start': 842.682, 'duration': 1.341}, {'end': 847.645, 'text': "so let's just go down to our value variable.", 'start': 844.023, 'duration': 3.622}, {'end': 850.927, 'text': "Okay, so here's our code, int value equals five.", 'start': 848.265, 'duration': 2.662}, {'end': 853.189, 'text': 'That is the CPU instruction that it runs.', 'start': 851.227, 'duration': 1.962}, {'end': 854.509, 'text': 'Now this is compiled in debug mode.', 'start': 853.249, 'duration': 1.26}, {'end': 855.07, 'text': 'keep that in mind.', 'start': 854.509, 'duration': 0.561}, {'end': 857.952, 'text': 'so there may be some extra things when it comes to the heap comparison.', 'start': 855.07, 'duration': 2.882}, {'end': 861.554, 'text': 'but for stack you can see all it does is it moves five into a register.', 'start': 857.952, 'duration': 3.602}, {'end': 865.897, 'text': "that's it done, or specifically into this kind of stack pointer at a specific offset?", 'start': 861.554, 'duration': 4.343}, {'end': 868.078, 'text': "But the idea is that's it.", 'start': 866.437, 'duration': 1.641}, {'end': 869.419, 'text': "It's one CPU instruction.", 'start': 868.378, 'duration': 1.041}, {'end': 873.901, 'text': 'Now with the array, we kind of have two things here and it actually does a bit of a multiplication, whatever.', 'start': 869.459, 'duration': 4.442}, {'end': 877.122, 'text': "I'm not going to talk about the semantics of that, but you can see what it's really just done.", 'start': 873.921, 'duration': 3.201}], 'summary': 'Analyzing assembly code reveals cpu instructions and stack manipulation.', 'duration': 48.81, 'max_score': 828.312, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/wJ1L2nSIV1s/pics/wJ1L2nSIV1s828312.jpg'}, {'end': 922.892, 'src': 'embed', 'start': 894.051, 'weight': 4, 'content': [{'end': 897.792, 'text': 'The main thing is it calls an entire operator and that operator new calls malloc,', 'start': 894.051, 'duration': 3.741}, {'end': 904.935, 'text': "and then that obviously has to go through the free list and check to see if we've got enough memory and gather the memory and record the fact that it's now been taken and how much has been allocated,", 'start': 897.792, 'duration': 7.143}, {'end': 906.876, 'text': "and then we have to delete it after we're done.", 'start': 904.935, 'duration': 1.941}, {'end': 911.438, 'text': 'Whole nightmare, right? And if we keep reading, you can see the same thing obviously happens for the array.', 'start': 907.496, 'duration': 3.942}, {'end': 916.36, 'text': 'And then finally, if we look at even deleting, the vector, first of all, is quite heavy.', 'start': 911.778, 'duration': 4.582}, {'end': 920.091, 'text': 'And it does call the constructor, of course, the same as our stack.', 'start': 917.489, 'duration': 2.602}, {'end': 922.892, 'text': 'The delete is incredibly heavy as well.', 'start': 921.051, 'duration': 1.841}], 'summary': 'The process of memory allocation and deallocation is heavy and inefficient.', 'duration': 28.841, 'max_score': 894.051, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/wJ1L2nSIV1s/pics/wJ1L2nSIV1s894051.jpg'}, {'end': 1013.318, 'src': 'embed', 'start': 977.196, 'weight': 3, 'content': [{'end': 980.557, 'text': "You'll have to allocate that on the heap and all that kind of stuff, right?", 'start': 977.196, 'duration': 3.361}, {'end': 988.659, 'text': "But if you can, you should be allocating on the stack all the time, because it's like one CPU instruction and that's very,", 'start': 980.957, 'duration': 7.702}, {'end': 990, 'text': 'very real performance difference.', 'start': 988.659, 'duration': 1.341}, {'end': 995.385, 'text': 'now one more time, just so that this is completely clear.', 'start': 990.862, 'duration': 4.523}, {'end': 997.807, 'text': 'the performance difference is the allocation.', 'start': 995.385, 'duration': 2.422}, {'end': 1013.318, 'text': "so if you were theoretically to pre-allocate i don't know a four gigabyte block of memory before you ran your program on the heap and then you were to kind of heap allocate from that pre-allocated four like gigabyte block of memory,", 'start': 997.807, 'duration': 15.511}], 'summary': 'Pre-allocating on the stack can yield significant performance differences compared to heap allocation.', 'duration': 36.122, 'max_score': 977.196, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/wJ1L2nSIV1s/pics/wJ1L2nSIV1s977196.jpg'}, {'end': 1098.366, 'src': 'embed', 'start': 1071.639, 'weight': 5, 'content': [{'end': 1077.561, 'text': "because it's very important for kind of real world applications and by real world applications it's been real time applications.", 'start': 1071.639, 'duration': 5.922}, {'end': 1084.742, 'text': "so it's really important for games essentially not to continuously allocate frame to frame, because that will be slow.", 'start': 1077.561, 'duration': 7.181}, {'end': 1090.904, 'text': 'so we have to basically come up with some clever memory management techniques if we want our game engine to actually be efficient,', 'start': 1084.742, 'duration': 6.162}, {'end': 1094.105, 'text': 'which is why we will definitely be discussing that in the game engine series.', 'start': 1090.904, 'duration': 3.201}, {'end': 1098.366, 'text': "Anyway, I hope I've answered all of your kind of stack-first heap questions.", 'start': 1094.725, 'duration': 3.641}], 'summary': 'Efficient game engine requires clever memory management techniques. real-time applications demand it.', 'duration': 26.727, 'max_score': 1071.639, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/wJ1L2nSIV1s/pics/wJ1L2nSIV1s1071639.jpg'}], 'start': 828.312, 'title': 'Understanding generated assembly code and stack vs heap allocation', 'summary': 'Discusses the process of compiling code to generated assembly, highlighting cpu instructions and operations involved, with a focus on stack and array manipulation. it also covers the differences between stack and heap allocation, emphasizing the performance benefits of allocating on the stack, with a focus on efficient memory management in game engines.', 'chapters': [{'end': 877.122, 'start': 828.312, 'title': 'Understanding generated assembly code', 'summary': 'Discusses the process of compiling code to generated assembly, highlighting the cpu instructions and operations involved, with a focus on stack and array manipulation.', 'duration': 48.81, 'highlights': ['The process of compiling code to generated assembly is demonstrated, showcasing the CPU instructions involved such as moving values into registers and stack pointers.', 'The impact of debug mode on the compilation process is mentioned, indicating the possibility of additional operations related to heap comparison.', 'The manipulation of array elements is briefly discussed, specifically mentioning the involvement of multiplication operations.']}, {'end': 1153.146, 'start': 877.122, 'title': 'Stack vs heap allocation', 'summary': 'Discusses the differences between stack and heap allocation, emphasizing the performance benefits of allocating on the stack whenever possible due to its minimal overhead, with a focus on real-world applications and efficient memory management in game engines.', 'duration': 276.024, 'highlights': ['The allocation on the stack is just immediate and results in minimal overhead, taking only one CPU instruction, making it a real performance difference compared to heap allocation. minimal overhead, one CPU instruction', 'Heap allocation involves a series of heavy processes, including calling operator new, which in turn calls malloc, leading to memory gathering, recording, and subsequent deletion after use. This results in a significant performance difference compared to stack allocation. heavy processes, significant performance difference', 'The video emphasizes the importance of allocating on the stack for real-world applications, particularly in game engines, to minimize continuous frame-to-frame allocations and ensure efficient memory management for improved performance. importance of stack allocation for game engines, efficient memory management']}], 'duration': 324.834, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/wJ1L2nSIV1s/pics/wJ1L2nSIV1s828312.jpg', 'highlights': ['The process of compiling code to generated assembly is demonstrated, showcasing the CPU instructions involved such as moving values into registers and stack pointers.', 'The impact of debug mode on the compilation process is mentioned, indicating the possibility of additional operations related to heap comparison.', 'The manipulation of array elements is briefly discussed, specifically mentioning the involvement of multiplication operations.', 'The allocation on the stack is just immediate and results in minimal overhead, taking only one CPU instruction, making it a real performance difference compared to heap allocation.', 'Heap allocation involves a series of heavy processes, including calling operator new, which in turn calls malloc, leading to memory gathering, recording, and subsequent deletion after use. This results in a significant performance difference compared to stack allocation.', 'The video emphasizes the importance of allocating on the stack for real-world applications, particularly in game engines, to minimize continuous frame-to-frame allocations and ensure efficient memory management for improved performance.']}], 'highlights': ['The stack and the heap are two key areas of memory in C++, with the stack having a predefined size of around two megabytes, while the heap can grow and change as the application progresses.', 'The process of finding a contiguous block of four bytes of memory differs between the stack and the heap.', 'The allocation on the stack is just immediate and results in minimal overhead, taking only one CPU instruction, making it a real performance difference compared to heap allocation.', 'The chapter explains accessing memory addresses in Visual Studio using the memory view, providing a visual representation of the memory address of a variable.', 'Stack memory allocated in a scope gets freed upon scope exit, making it efficient and cost-free compared to heap memory.', 'The difference in memory allocation speed between the stack and the heap is emphasized, with stack allocation being just one CPU instruction compared to the complexity of heap allocation.', 'The process of compiling code to generated assembly is demonstrated, showcasing the CPU instructions involved such as moving values into registers and stack pointers.', 'The impact of debug mode on the compilation process is mentioned, indicating the possibility of additional operations related to heap comparison.', "The allocation of integer, array, and object data types on both stack and heap, emphasizing the differences and the use of the 'new' keyword for heap allocation.", 'The chapter demonstrates the process of initializing a variable in debug mode, showcasing the practical application of debugging.', 'The stack allocation in C++ allows for variables to be stored closely together in memory due to the stack pointer moving a specific amount of bytes for each variable, resulting in extremely fast allocation and one CPU instruction.', 'Heap allocation involves a series of heavy processes, including calling operator new, which in turn calls malloc, leading to memory gathering, recording, and subsequent deletion after use. This results in a significant performance difference compared to stack allocation.']}