title
VECTOR/DYNAMIC ARRAY - Making DATA STRUCTURES in C++

description
The first 1000 people who click the link will get 2 free months of Skillshare Premium: https://skl.sh/thecherno0820 Patreon ► https://patreon.com/thecherno Instagram ► https://instagram.com/thecherno Twitter ► https://twitter.com/thecherno Discord ► https://thecherno.com/discord Series Playlist ► https://thecherno.com/cpp This video was sponsored by Skillshare.

detail
{'title': 'VECTOR/DYNAMIC ARRAY - Making DATA STRUCTURES in C++', 'heatmap': [{'end': 1419.853, 'start': 1335.065, 'weight': 0.917}, {'end': 1530.265, 'start': 1497.258, 'weight': 0.796}, {'end': 1747.473, 'start': 1661.592, 'weight': 0.768}, {'end': 2018.604, 'start': 1989.255, 'weight': 0.705}, {'end': 2428.469, 'start': 2365.053, 'weight': 1}, {'end': 2563.621, 'start': 2506.553, 'weight': 0.809}], 'summary': 'Tutorial series on making data structures in c++ covers implementing dynamic arrays, optimizing memory reallocation, and introducing vector classes, emphasizing the importance of optimization for performance benefits and offering a skillshare sponsorship for the first 1000 sign-ups.', 'chapters': [{'end': 134.405, 'segs': [{'end': 64.299, 'src': 'embed', 'start': 39.246, 'weight': 0, 'content': [{'end': 44.551, 'text': "If you're not familiar with STD vector, I suggest that you check out the videos that I've made on that topic already.", 'start': 39.246, 'duration': 5.305}, {'end': 51.294, 'text': "Today, we're going to be taking a look at how we can write something similar to the SCD vector class.", 'start': 45.091, 'duration': 6.203}, {'end': 53.394, 'text': "We're going to be writing this ourselves from scratch,", 'start': 51.414, 'duration': 1.98}, {'end': 58.196, 'text': 'with the purpose of learning a little bit more about data structures and about how this vector class works.', 'start': 53.394, 'duration': 4.802}, {'end': 64.299, 'text': 'And, as such, our goal is to essentially replicate the behavior, the same behavior of that SCD vector class,', 'start': 58.757, 'duration': 5.542}], 'summary': 'Learn to replicate scd vector class behavior from scratch.', 'duration': 25.053, 'max_score': 39.246, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC039246.jpg'}, {'end': 140.248, 'src': 'embed', 'start': 115.753, 'weight': 1, 'content': [{'end': 123.799, 'text': "And coming in at just under $10 a month for an annual subscription, it's a great way to add a few more skills into your lives.", 'start': 115.753, 'duration': 8.046}, {'end': 132.044, 'text': 'Skillshare have been nice enough to offer the first 1000 people who sign up using the link in the description below two free months of Skillshare premium.', 'start': 124.019, 'duration': 8.025}, {'end': 134.405, 'text': 'two months to learn whatever you like.', 'start': 132.044, 'duration': 2.361}, {'end': 140.248, 'text': 'Check out the link in description below guys and take a look at all the different classes that Skillshare has to offer.', 'start': 134.845, 'duration': 5.403}], 'summary': 'Skillshare offers annual subscription for under $10, with first 1000 sign-ups receiving two free months of premium.', 'duration': 24.495, 'max_score': 115.753, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC0115753.jpg'}], 'start': 0.009, 'title': 'Implementing dynamic arrays in c++', 'summary': 'Explores implementing a dynamic array in c++, focusing on replicating the behavior of the std vector class. it is sponsored by skillshare offering two free months of premium subscription for the first 1000 sign-ups.', 'chapters': [{'end': 134.405, 'start': 0.009, 'title': 'Implementing dynamic arrays in c++', 'summary': 'Explores implementing a dynamic array in c++, focusing on replicating the behavior of the std vector class, and is sponsored by skillshare offering two free months of premium subscription for the first 1000 sign-ups.', 'duration': 134.396, 'highlights': ['The chapter focuses on replicating the behavior of the STD vector class in C++, providing a deeper understanding of data structures and the vector class functionality. The goal is to replicate the behavior of the STD vector class, allowing for more or less interchangeability, providing a deeper understanding of data structures and vector class functionality.', 'The chapter is sponsored by Skillshare and offers two free months of premium subscription for the first 1000 sign-ups, providing an opportunity for viewers to access high-quality, bite-sized classes across various topics. Skillshare is a massive online learning community offering high-quality, bite-sized classes across various topics, and is offering two free months of premium subscription for the first 1000 sign-ups.', 'Skillshare offers thousands of different classes, with most classes being high quality and bite-sized, and is available for just under $10 a month for an annual subscription. Skillshare offers thousands of different classes, with most classes being high quality and bite-sized, and is available for just under $10 a month for an annual subscription.']}], 'duration': 134.396, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC09.jpg', 'highlights': ['The chapter focuses on replicating the behavior of the STD vector class in C++, providing a deeper understanding of data structures and the vector class functionality.', 'The chapter is sponsored by Skillshare and offers two free months of premium subscription for the first 1000 sign-ups, providing an opportunity for viewers to access high-quality, bite-sized classes across various topics.', 'Skillshare offers thousands of different classes, with most classes being high quality and bite-sized, and is available for just under $10 a month for an annual subscription.']}, {'end': 412.806, 'segs': [{'end': 205.026, 'src': 'embed', 'start': 167.848, 'weight': 0, 'content': [{'end': 176.073, 'text': 'And as such, even though something as simple as a vector or an array might seem really simple, you can actually extend it quite a lot.', 'start': 167.848, 'duration': 8.225}, {'end': 178.054, 'text': 'I mean, take a look at the standard template library code.', 'start': 176.133, 'duration': 1.921}, {'end': 183.057, 'text': "It's so big and complex because it deals with every little corner case.", 'start': 178.194, 'duration': 4.863}, {'end': 187.039, 'text': 'It deals really with everything and it supports all of the newest language features.', 'start': 183.497, 'duration': 3.542}, {'end': 193.583, 'text': "So even if you think writing a vector might be a little bit too simple for you, think again, because I'm sure you can get a lot out of it.", 'start': 187.399, 'duration': 6.184}, {'end': 201.505, 'text': 'I also think that a good way to follow this series as we start exploring more and more data structures is to try and write it on your own.', 'start': 193.743, 'duration': 7.762}, {'end': 205.026, 'text': 'Pause the video right now and try and implement your own vector class first.', 'start': 201.565, 'duration': 3.461}], 'summary': 'Vectors can be extended a lot, as seen in the standard library, supporting all language features. try implementing your own vector class.', 'duration': 37.178, 'max_score': 167.848, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC0167848.jpg'}, {'end': 248.369, 'src': 'embed', 'start': 223.05, 'weight': 1, 'content': [{'end': 230.396, 'text': "So what is this vector data structure? Well, I mentioned that it is a resizable array, and that's probably the biggest thing to note here.", 'start': 223.05, 'duration': 7.346}, {'end': 237.381, 'text': 'But the other huge thing that I think of when I think of the STD vector class is that the data there is heap allocated.', 'start': 230.736, 'duration': 6.645}, {'end': 243.465, 'text': 'This is a direct contrast to what we had with the standard array class because that was completely on the stack.', 'start': 237.481, 'duration': 5.984}, {'end': 245.347, 'text': 'As such, it was completely fixed.', 'start': 243.886, 'duration': 1.461}, {'end': 248.369, 'text': 'It was not only fixed, but we had to actually determine the size.', 'start': 245.527, 'duration': 2.842}], 'summary': 'Vector data structure is a resizable array with heap allocated data, contrasting with the fixed size of standard array class.', 'duration': 25.319, 'max_score': 223.05, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC0223050.jpg'}, {'end': 287.441, 'src': 'embed', 'start': 260.94, 'weight': 5, 'content': [{'end': 267.465, 'text': "But even so, we don't have to think about the size at compile time because we just push back more and more data, it resizes.", 'start': 260.94, 'duration': 6.525}, {'end': 270.688, 'text': 'Everything there is dynamic and runtime.', 'start': 268.186, 'duration': 2.502}, {'end': 272.129, 'text': 'That is a huge difference.', 'start': 270.848, 'duration': 1.281}, {'end': 277.233, 'text': 'And because of that there are so many reasons why you would want to choose the array class over the vector class,', 'start': 272.289, 'duration': 4.944}, {'end': 280.656, 'text': "because if you don't need heap allocations, then don't use them.", 'start': 277.233, 'duration': 3.423}, {'end': 282.057, 'text': "They're going to slow down your program.", 'start': 280.696, 'duration': 1.361}, {'end': 287.441, 'text': 'So with that in mind, the core mechanics of a vector class really are quite simple if you think about them.', 'start': 282.257, 'duration': 5.184}], 'summary': 'Choose array over vector for performance; avoid unnecessary heap allocations.', 'duration': 26.501, 'max_score': 260.94, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC0260940.jpg'}, {'end': 365.96, 'src': 'embed', 'start': 339.599, 'weight': 2, 'content': [{'end': 346.946, 'text': "That kind of sounds not that great for performance, right? Right, that's why vector resizing and all these heap allocations really aren't great.", 'start': 339.599, 'duration': 7.347}, {'end': 348.847, 'text': 'We might explore them in more depth a little bit later.', 'start': 346.966, 'duration': 1.881}, {'end': 356.832, 'text': 'I did actually make a video about optimizing the usage of std vector to try and avoid these resizes, because every time we have to grow our array,', 'start': 348.948, 'duration': 7.884}, {'end': 357.773, 'text': 'we have to grow our vector.', 'start': 356.832, 'duration': 0.941}, {'end': 365.96, 'text': "That's usually just not great for performance and we want to avoid it, especially if our vector happens to have a lot of elements inside it.", 'start': 358.473, 'duration': 7.487}], 'summary': 'Heap allocations and vector resizing can impact performance, so optimizing std vector usage is crucial to avoid performance issues.', 'duration': 26.361, 'max_score': 339.599, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC0339599.jpg'}], 'start': 134.845, 'title': 'Writing and overview of vector class', 'summary': 'Discusses the importance of writing a vector class to understand data structures and improve programming skills. it also introduces the std vector class as a resizable array with heap-allocated data, emphasizing the need to optimize vector resizing for performance benefits.', 'chapters': [{'end': 222.87, 'start': 134.845, 'title': 'Writing a vector class', 'summary': 'Discusses the importance of writing a vector class to gain a deeper understanding of data structures and improve programming skills, emphasizing the value of creating a vector class and the benefits of exploring data structures on your own.', 'duration': 88.025, 'highlights': ['Creating a vector class can enhance programming skills and understanding of data structures, providing a valuable learning experience.', 'The standard template library code is extensive and complex, supporting all language features and corner cases, illustrating the potential depth and complexity of data structures.', 'Encouraging viewers to pause the video and attempt to implement their own vector class before comparing solutions serves as a practical and effective learning exercise.']}, {'end': 412.806, 'start': 223.05, 'title': 'Std vector class overview', 'summary': 'Introduces the std vector class as a resizable array with heap-allocated data, contrasting it with the standard array class, highlighting the dynamic and runtime nature of vector, and emphasizing the need to optimize vector resizing for performance benefits.', 'duration': 189.756, 'highlights': ['The STD vector class is a resizable array with heap-allocated data, contrasting it with the standard array class. The STD vector class is a resizable array with heap-allocated data, providing dynamic and runtime capabilities, unlike the standard array class which is fixed and stack-allocated.', 'Emphasizing the need to optimize vector resizing for performance benefits. Optimizing vector resizing, such as using move semantics instead of copying elements, can provide significant performance benefits, especially when dealing with a large number of elements.', 'Highlighting the dynamic and runtime nature of vector, emphasizing the need to avoid unnecessary heap allocations for performance optimization. The vector class provides dynamic and runtime capabilities, allowing for on-demand resizing without the need to determine size at compile time, but unnecessary heap allocations may lead to performance slowdowns, necessitating optimization.']}], 'duration': 277.961, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC0134845.jpg', 'highlights': ['Creating a vector class enhances programming skills and understanding of data structures.', 'The STD vector class is a resizable array with heap-allocated data, providing dynamic and runtime capabilities.', 'Emphasizing the need to optimize vector resizing for performance benefits, especially when dealing with a large number of elements.', 'Encouraging viewers to pause the video and attempt to implement their own vector class before comparing solutions serves as a practical and effective learning exercise.', 'The standard template library code is extensive and complex, supporting all language features and corner cases, illustrating the potential depth and complexity of data structures.', 'Highlighting the dynamic and runtime nature of vector, emphasizing the need to avoid unnecessary heap allocations for performance optimization.']}, {'end': 1189.772, 'segs': [{'end': 517.167, 'src': 'embed', 'start': 492.716, 'weight': 3, 'content': [{'end': 499.419, 'text': "I'll add a third element here because as you'll see in a minute, we're actually going to initialize our vector to have a size of two by default.", 'start': 492.716, 'duration': 6.703}, {'end': 504.982, 'text': "Okay, So, now that we have a very basic example, let's jump into this vector.h file, which is completely blank,", 'start': 499.539, 'duration': 5.443}, {'end': 506.763, 'text': 'and take a look at how we would write this.', 'start': 504.982, 'duration': 1.781}, {'end': 511.945, 'text': "So obviously it's going to be a templated type here, a templated class, I should say.", 'start': 507.383, 'duration': 4.562}, {'end': 517.167, 'text': 'So last time when we wrote this array class, we actually kind of made a very simple version of it without any templates.', 'start': 512.164, 'duration': 5.003}], 'summary': 'Discussing vector initialization and templated class in vector.h file.', 'duration': 24.451, 'max_score': 492.716, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC0492716.jpg'}, {'end': 728.697, 'src': 'embed', 'start': 696.761, 'weight': 1, 'content': [{'end': 700.584, 'text': "but we're going to do something a little bit more conservative in terms of space.", 'start': 696.761, 'duration': 3.823}, {'end': 704.566, 'text': "What we're going to do is add on half of the current capacity.", 'start': 700.664, 'duration': 3.902}, {'end': 712.51, 'text': "So, in other words, we'll grow from two to three, from three to four, from four to six, from six to nine, and so on and so forth,", 'start': 704.606, 'duration': 7.904}, {'end': 714.511, 'text': "just because it's a little bit more conservative on memory.", 'start': 712.51, 'duration': 2.001}, {'end': 717.252, 'text': 'But this is the beauty of writing your own data structures.', 'start': 714.991, 'duration': 2.261}, {'end': 721.995, 'text': 'You can actually easily control that to tailor it towards a specific need in your application.', 'start': 717.292, 'duration': 4.703}, {'end': 728.697, 'text': "Maybe you know that you're going to be pushing back a lot of elements and you want it to be a little bit more generous with the amount of memory it allocates.", 'start': 722.035, 'duration': 6.662}], 'summary': 'Increasing capacity by 50% for better memory management in data structures.', 'duration': 31.936, 'max_score': 696.761, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC0696761.jpg'}, {'end': 860.631, 'src': 'embed', 'start': 837.833, 'weight': 2, 'content': [{'end': 845.838, 'text': "Now I'm saying copy, but realistically you want to try and move them for sure, because copying them is going to be not only a waste of performance,", 'start': 837.833, 'duration': 8.005}, {'end': 852.402, 'text': "it's just going to be unnecessary, because clearly we're going to be deleting the old block of memory, which is our third step.", 'start': 845.838, 'duration': 6.564}, {'end': 856.546, 'text': 'So copy slash move old elements into new block.', 'start': 852.862, 'duration': 3.684}, {'end': 859.069, 'text': 'This is what our reallocation function looks like.', 'start': 856.706, 'duration': 2.363}, {'end': 860.631, 'text': "So now let's implement it.", 'start': 859.449, 'duration': 1.182}], 'summary': 'Reallocation function: move old elements into new block for performance efficiency.', 'duration': 22.798, 'max_score': 837.833, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC0837833.jpg'}, {'end': 1116.822, 'src': 'embed', 'start': 1089.123, 'weight': 0, 'content': [{'end': 1094.948, 'text': 'So to do that, we simply check to see if size is greater than or equal to the capacity.', 'start': 1089.123, 'duration': 5.825}, {'end': 1102.093, 'text': "So in other words, if we're at capacity or somehow over it, then we need to reallocate with some kind of new capacity.", 'start': 1095.128, 'duration': 6.965}, {'end': 1104.255, 'text': "So to do that, we'll simply call realloc.", 'start': 1102.213, 'duration': 2.042}, {'end': 1110.32, 'text': "And as mentioned before, what we're going to do is we're going to take our capacity and add half of our capacity to it.", 'start': 1104.775, 'duration': 5.545}, {'end': 1116.822, 'text': "So we're basically growing this by 1.5 times every time we need to grow, which is effectively growing by 50%.", 'start': 1110.64, 'duration': 6.182}], 'summary': 'To grow the capacity, realloc is called and capacity is increased by 50%.', 'duration': 27.699, 'max_score': 1089.123, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC01089123.jpg'}], 'start': 412.926, 'title': 'Vector class implementation and reallocation', 'summary': 'Introduces a basic implementation of a vector class, covering the concept of size and capacity, and discusses reallocation and pushback functions, including a 50% reallocation, aiming to efficiently manage memory and support dynamic resizing.', 'chapters': [{'end': 803.518, 'start': 412.926, 'title': 'Basic vector class implementation', 'summary': 'Introduces a basic implementation of a vector class, including the concept of size and capacity, the reallocation of memory, and the conservative approach of adding half of the current capacity to handle memory allocation efficiently.', 'duration': 390.592, 'highlights': ['The vector class is introduced with just the base features, focusing on a templated type, constructor, and the concept of size and capacity. The chapter introduces a basic implementation of a vector class, including the concept of size and capacity, and the use of a templated type and constructor.', "The concept of reallocation of memory is explained, emphasizing the need for a private function called 'reallocate' to allocate a new block of memory. The chapter explains the need for a private function called 'reallocate' to allocate a new block of memory for reallocation of memory in the vector class.", 'The conservative approach of adding half of the current capacity for memory allocation is discussed to efficiently handle memory reallocation. The chapter discusses the conservative approach of adding half of the current capacity for memory allocation in the vector class to efficiently handle memory reallocation.']}, {'end': 1189.772, 'start': 803.678, 'title': 'Vector reallocation and pushback implementation', 'summary': 'Discusses the implementation of reallocation and pushback functions for a vector class, including resizing, reallocation by 50%, and index operator implementation, aiming to efficiently manage memory and support dynamic resizing.', 'duration': 386.094, 'highlights': ['The reallocation function involves copying or moving old elements into a new block of memory, which is allocated based on the new capacity, with a focus on efficient memory management. The reallocation function involves copying or moving old elements into a new block of memory, which is allocated based on the new capacity, with a focus on efficient memory management.', 'The pushback function involves checking if the current size exceeds the capacity and reallocating with a new capacity of 1.5 times the original, effectively growing by 50% to support dynamic resizing. The pushback function involves checking if the current size exceeds the capacity and reallocating with a new capacity of 1.5 times the original, effectively growing by 50% to support dynamic resizing.', 'The implementation includes the index operator, with considerations for bounds checking, although the specific handling of out-of-bounds access is not included in the current version. The implementation includes the index operator, with considerations for bounds checking, although the specific handling of out-of-bounds access is not included in the current version.']}], 'duration': 776.846, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC0412926.jpg', 'highlights': ['The pushback function involves checking if the current size exceeds the capacity and reallocating with a new capacity of 1.5 times the original, effectively growing by 50% to support dynamic resizing.', 'The conservative approach of adding half of the current capacity for memory allocation is discussed to efficiently handle memory reallocation.', 'The reallocation function involves copying or moving old elements into a new block of memory, which is allocated based on the new capacity, with a focus on efficient memory management.', 'The vector class is introduced with just the base features, focusing on a templated type, constructor, and the concept of size and capacity.']}, {'end': 1457.271, 'segs': [{'end': 1296.984, 'src': 'embed', 'start': 1261.117, 'weight': 0, 'content': [{'end': 1268.719, 'text': 'if we put a little breakpoint over here, just so that we can see where our vector is at, we can see that we have a size of 8, a capacity of 9,', 'start': 1261.117, 'duration': 7.602}, {'end': 1274.1, 'text': 'of course, because it went from 6 to 9 and everything seems pretty good to me.', 'start': 1268.719, 'duration': 5.381}, {'end': 1280.621, 'text': "okay. so let's talk a little bit about this whole copying thing and also write a new function called in place back, which is a very,", 'start': 1274.1, 'duration': 6.521}, {'end': 1283.602, 'text': 'very useful function to have inside our vector class.', 'start': 1280.621, 'duration': 2.981}, {'end': 1287.822, 'text': "What I'll do to demonstrate this is I'll simply write a struct here called vector3,,", 'start': 1283.742, 'duration': 4.08}, {'end': 1291.103, 'text': 'which is going to be like a mathematical kind of three component vector here.', 'start': 1287.822, 'duration': 3.281}, {'end': 1296.984, 'text': "x, y, z, we're going to write a constructor here that takes in, well, let's just write a default constructor that does nothing.", 'start': 1291.603, 'duration': 5.381}], 'summary': 'Demonstrated vector with size 8, capacity 9. introduced new in-place back function.', 'duration': 35.867, 'max_score': 1261.117, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC01261117.jpg'}, {'end': 1419.853, 'src': 'heatmap', 'start': 1335.065, 'weight': 0.917, 'content': [{'end': 1339.047, 'text': "And I'll also write a destructor here, which is going to say destroy.", 'start': 1335.065, 'duration': 3.982}, {'end': 1342.049, 'text': "And then finally I'll write a move constructor here.", 'start': 1339.147, 'duration': 2.902}, {'end': 1346.953, 'text': 'So now of course we should be able to detect copies, moves and destroys, which is pretty cool.', 'start': 1342.189, 'duration': 4.764}, {'end': 1349.794, 'text': "Last thing we're kind of missing is the constructor, but we won't worry about that.", 'start': 1346.993, 'duration': 2.801}, {'end': 1353.017, 'text': "So let's change this over to actually be our vector.", 'start': 1350.235, 'duration': 2.782}, {'end': 1354.558, 'text': 'So we now have a vector of vector threes.', 'start': 1353.037, 'duration': 1.521}, {'end': 1356.639, 'text': "I'm gonna go ahead and create some of these.", 'start': 1354.758, 'duration': 1.881}, {'end': 1359.141, 'text': 'So maybe one of these will be a scalar.', 'start': 1356.699, 'duration': 2.442}, {'end': 1362.022, 'text': "I'll remove some of these just to make this less complex.", 'start': 1359.761, 'duration': 2.261}, {'end': 1365.323, 'text': 'One of them will take in X, Y, Z here.', 'start': 1362.262, 'duration': 3.061}, {'end': 1369.004, 'text': 'And then one of them maybe will just be a default constructor.', 'start': 1365.863, 'duration': 3.141}, {'end': 1371.225, 'text': "So we have three of these, let's hit F5 and see what we get.", 'start': 1369.024, 'duration': 2.201}, {'end': 1376.886, 'text': "Now in my haste here, I of course didn't actually provide any assignment or move operators.", 'start': 1371.325, 'duration': 5.561}, {'end': 1377.807, 'text': "So let's quickly do that.", 'start': 1376.906, 'duration': 0.901}, {'end': 1380.908, 'text': "And now with these two assignment operators present, let's hit F5.", 'start': 1377.967, 'duration': 2.941}, {'end': 1386.129, 'text': "We need to also make sure that we print vector three correctly because there's no way to just print it like a string like this.", 'start': 1381.088, 'duration': 5.041}, {'end': 1387.49, 'text': 'So to make that happen,', 'start': 1386.329, 'duration': 1.161}, {'end': 1395.836, 'text': "We'll just copy and paste this and write a specialization for our templated print function here that just takes in a vector of vector threes specifically.", 'start': 1387.97, 'duration': 7.866}, {'end': 1398.098, 'text': "And we'll print X, Y, and Z.", 'start': 1396.036, 'duration': 2.062}, {'end': 1398.658, 'text': 'Okay, cool.', 'start': 1398.098, 'duration': 0.56}, {'end': 1400.679, 'text': 'Now if we hit a five, everything should work.', 'start': 1398.838, 'duration': 1.841}, {'end': 1405.883, 'text': 'And of course, we need to make sure that this print vector function is actually underneath this struct declaration.', 'start': 1400.82, 'duration': 5.063}, {'end': 1408.285, 'text': 'Otherwise, it has no idea what a vector three is.', 'start': 1406.324, 'duration': 1.961}, {'end': 1409.366, 'text': "Okay, let's hit a five.", 'start': 1408.445, 'duration': 0.921}, {'end': 1410.867, 'text': 'Everything should now finally work.', 'start': 1409.426, 'duration': 1.441}, {'end': 1412.888, 'text': 'All right, so we have a lot of stuff happening here.', 'start': 1410.987, 'duration': 1.901}, {'end': 1418.012, 'text': 'We have a copy, we have a destroy, we have a copy, copy, destroy, destroy, copy, destroy, lots of stuff going on.', 'start': 1412.928, 'duration': 5.084}, {'end': 1419.853, 'text': 'But finally, of course, we get this result.', 'start': 1418.212, 'duration': 1.641}], 'summary': 'The transcript involves creating constructors, move and destroy operations for a vector of vector threes, and writing a specialization for a templated print function.', 'duration': 84.788, 'max_score': 1335.065, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC01335065.jpg'}, {'end': 1469.444, 'src': 'embed', 'start': 1440.144, 'weight': 3, 'content': [{'end': 1445.246, 'text': "And it's also not something that's even worth optimizing because this is just simply three floats.", 'start': 1440.144, 'duration': 5.102}, {'end': 1449.628, 'text': 'Copying three floats, which is 12 bytes of memory is never really a problem.', 'start': 1445.386, 'duration': 4.242}, {'end': 1452.429, 'text': 'You would probably never want to optimize that,', 'start': 1449.928, 'duration': 2.501}, {'end': 1457.271, 'text': 'but just think of vector three as potentially a string or something that might actually have a heap allocation.', 'start': 1452.429, 'duration': 4.842}, {'end': 1464.619, 'text': 'Every time we do copy, we have to reallocate that heap memory somewhere else and then free the old one.', 'start': 1457.791, 'duration': 6.828}, {'end': 1469.444, 'text': 'This is obviously not very good for performance and there are ways to improve that.', 'start': 1464.799, 'duration': 4.645}], 'summary': 'Copying three floats, 12 bytes memory, potential impact on performance, ways to improve.', 'duration': 29.3, 'max_score': 1440.144, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC01440144.jpg'}], 'start': 1190.132, 'title': 'Vector class implementation and optimization', 'summary': 'Discusses the implementation and testing of a simple vector class in a c++ environment, showcasing the process of adding elements, creating a template function for printing the vector, and testing its functionality with breakpoints, resulting in successful operations. it also covers the implementation of a vector class, including constructors, copy constructor, destructor, move constructor, assignment operators, and optimizing copies and destroys, emphasizing that copying three floats, which is 12 bytes of memory, is not worth optimizing.', 'chapters': [{'end': 1280.621, 'start': 1190.132, 'title': 'Vector class and debug builds', 'summary': 'Discusses the implementation and testing of a simple vector class in a c++ environment, showcasing the process of adding elements, creating a template function for printing the vector, and testing its functionality with breakpoints, resulting in successful operations.', 'duration': 90.489, 'highlights': ['The chapter discusses the implementation and testing of a simple vector class in a C++ environment It covers the process of adding elements, creating a template function for printing the vector, and testing its functionality with breakpoints.', 'The vector class is successfully tested with a size of 8 and a capacity of 9 The successful testing of the vector class is demonstrated with specific size and capacity values, indicating its functionality.', 'The concept of in place back function is introduced The chapter introduces the concept of in place back function, expanding the discussion to include the copying process.']}, {'end': 1457.271, 'start': 1280.621, 'title': 'Vector class optimization', 'summary': 'Discusses the implementation of a vector class, including constructors, copy constructor, destructor, move constructor, assignment operators, and optimizing copies and destroys, emphasizing that copying three floats, which is 12 bytes of memory, is not worth optimizing.', 'duration': 176.65, 'highlights': ['The chapter discusses the implementation of a vector class, including constructors, copy constructor, destructor, move constructor, assignment operators, and optimizing copies and destroys. It covers the implementation details of a vector class, including various constructors, copy constructor, destructor, move constructor, and assignment operators.', 'Emphasizes that copying three floats, which is 12 bytes of memory, is not worth optimizing. The speaker stresses the inefficiency of optimizing the copying of three floats, which amounts to 12 bytes of memory, indicating that it is not a significant optimization concern.']}], 'duration': 267.139, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC01190132.jpg', 'highlights': ['The chapter discusses the implementation of a vector class, including constructors, copy constructor, destructor, move constructor, and assignment operators.', 'The vector class is successfully tested with a size of 8 and a capacity of 9, demonstrating its functionality.', 'The concept of in place back function is introduced, expanding the discussion to include the copying process.', 'Emphasizes that copying three floats, which is 12 bytes of memory, is not worth optimizing.']}, {'end': 1740.068, 'segs': [{'end': 1489.334, 'src': 'embed', 'start': 1457.791, 'weight': 3, 'content': [{'end': 1464.619, 'text': 'Every time we do copy, we have to reallocate that heap memory somewhere else and then free the old one.', 'start': 1457.791, 'duration': 6.828}, {'end': 1469.444, 'text': 'This is obviously not very good for performance and there are ways to improve that.', 'start': 1464.799, 'duration': 4.645}, {'end': 1471.005, 'text': "And that's what this is going to be about.", 'start': 1469.544, 'duration': 1.461}, {'end': 1475.27, 'text': "So first step, you'll notice here that all of these are actually temporaries.", 'start': 1471.446, 'duration': 3.824}, {'end': 1479.711, 'text': "These are not variables that exist elsewhere and then finally we're deciding to push them back into a vector, no.", 'start': 1475.45, 'duration': 4.261}, {'end': 1483.413, 'text': "The only reason they're here is because we want to basically give them to the vector.", 'start': 1479.831, 'duration': 3.582}, {'end': 1489.334, 'text': 'So one thing that we could do is write a different pushback function that actually takes in a temporary.', 'start': 1483.773, 'duration': 5.561}], 'summary': 'Copying heap memory impacts performance. improvements can be made by implementing a different pushback function.', 'duration': 31.543, 'max_score': 1457.791, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC01457791.jpg'}, {'end': 1540.056, 'src': 'heatmap', 'start': 1497.258, 'weight': 1, 'content': [{'end': 1502.762, 'text': "except instead of just simply assigning mdata to value, we're going to actually move it in there.", 'start': 1497.258, 'duration': 5.504}, {'end': 1507.465, 'text': 'Now, move just simply casts this value to be an rvalue reference.', 'start': 1503.142, 'duration': 4.323}, {'end': 1512.508, 'text': "You can see, it's already an rvalue reference, but there's a bit of a trick which is, as soon as you actually enter a function,", 'start': 1507.485, 'duration': 5.023}, {'end': 1515.09, 'text': "if it's a parameter like this, this now becomes an lvalue.", 'start': 1512.508, 'duration': 2.582}, {'end': 1520.514, 'text': "And since it's an lvalue, we need to be like, no, no, no, I want this to be an rvalue, seriously, for the purposes of moving this in here.", 'start': 1515.17, 'duration': 5.344}, {'end': 1525.259, 'text': 'So now, if this overload is used which it will be for all of these cases,', 'start': 1520.934, 'duration': 4.325}, {'end': 1530.265, 'text': "because they're all R values we should not see any copy coming from this actual pushback function.", 'start': 1525.259, 'duration': 5.006}, {'end': 1531.406, 'text': 'It should be moving it.', 'start': 1530.425, 'duration': 0.981}, {'end': 1536.312, 'text': "And if you're confused about L values and R values, as well as move semantics, all that, I have a bunch of videos about that.", 'start': 1531.506, 'duration': 4.806}, {'end': 1538.134, 'text': "I'll link them in the top right corner.", 'start': 1536.672, 'duration': 1.462}, {'end': 1539.195, 'text': 'Definitely check them out.', 'start': 1538.274, 'duration': 0.921}, {'end': 1540.056, 'text': "Let's see what we get here.", 'start': 1539.235, 'duration': 0.821}], 'summary': 'Using move semantics to avoid unnecessary copies in pushback function for r values.', 'duration': 42.798, 'max_score': 1497.258, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC01497258.jpg'}, {'end': 1631.082, 'src': 'embed', 'start': 1609.649, 'weight': 0, 'content': [{'end': 1618.876, 'text': 'well, instead of constructing this vector3 instance over here inside the stack frame of main and then moving it into that data storage we have inside vector,', 'start': 1609.649, 'duration': 9.227}, {'end': 1620.377, 'text': "which is what we're currently doing,", 'start': 1618.876, 'duration': 1.501}, {'end': 1629.721, 'text': "why don't you just give me all of the arguments that I need to actually construct a vector3 and I'll do the construction right there in place in my data block.", 'start': 1620.377, 'duration': 9.344}, {'end': 1631.082, 'text': "That's what in place back does.", 'start': 1629.761, 'duration': 1.321}], 'summary': 'Using in place back to construct vector3 in data block.', 'duration': 21.433, 'max_score': 1609.649, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC01609649.jpg'}, {'end': 1698.601, 'src': 'embed', 'start': 1669.795, 'weight': 2, 'content': [{'end': 1674.02, 'text': "We'll actually return a T reference here, because since we're constructing something in place,", 'start': 1669.795, 'duration': 4.225}, {'end': 1676.263, 'text': "it's not really that easy for us to get that object back.", 'start': 1674.02, 'duration': 2.243}, {'end': 1679.487, 'text': 'So usually these functions return a T reference.', 'start': 1676.723, 'duration': 2.764}, {'end': 1681.049, 'text': "I'll call it in place back, of course.", 'start': 1679.667, 'duration': 1.382}, {'end': 1683.832, 'text': "And now we'll just take in all of our arguments here.", 'start': 1681.189, 'duration': 2.643}, {'end': 1686.514, 'text': 'Now I might make a video in the future about variadic templates.', 'start': 1683.952, 'duration': 2.562}, {'end': 1691.017, 'text': "And when I do, I'll expand a lot more on them because honestly, they definitely fill up a video.", 'start': 1686.694, 'duration': 4.323}, {'end': 1694.879, 'text': "So we'll make sure that we have our same reallocation behavior.", 'start': 1691.397, 'duration': 3.482}, {'end': 1698.601, 'text': "And then we'll do mdata msize equals t.", 'start': 1695.019, 'duration': 3.582}], 'summary': 'Returning a t reference for in-place construction, with plans for future video expansion.', 'duration': 28.806, 'max_score': 1669.795, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC01669795.jpg'}], 'start': 1457.791, 'title': 'Memory reallocation optimization and rvalue references', 'summary': 'Discusses inefficiencies in heap memory reallocation, emphasizing the need for optimizing the pushback function. it also explains rvalue reference and move semantics, showcasing their usage to minimize unnecessary copies and optimize memory allocation, and introduces the in-place back function using variadic templates.', 'chapters': [{'end': 1489.334, 'start': 1457.791, 'title': 'Optimizing memory reallocation', 'summary': 'Discusses the inefficiency of reallocation of heap memory during copy operations, highlighting the need for optimizing the pushback function to handle temporary variables.', 'duration': 31.543, 'highlights': ['The inefficiency of reallocation of heap memory during copy operations is discussed, emphasizing the need for optimization.', 'The importance of optimizing the pushback function to handle temporary variables is highlighted.']}, {'end': 1740.068, 'start': 1489.775, 'title': 'Rvalue reference and move semantics', 'summary': 'Explains the concept of rvalue reference and move semantics, demonstrating its usage to minimize unnecessary copies and optimize memory allocation in a vector class, while also introducing the in-place back function using variadic templates.', 'duration': 250.293, 'highlights': ['The chapter explains the concept of Rvalue reference and move semantics, demonstrating its usage to minimize unnecessary copies and optimize memory allocation in a vector class The chapter covers the concept of Rvalue reference and move semantics, showcasing how it minimizes copies and optimizes memory allocation in a vector class by utilizing move semantics to transfer resources, reducing unnecessary copying.', 'Introducing the in-place back function using variadic templates The chapter introduces the in-place back function using variadic templates, allowing for the construction of objects in place within the vector class, showcasing how this function eliminates the need to construct objects in the stack frame of main and then move them into the data storage of the vector.', 'Demonstration of reallocation behavior and forwarding of arguments to the constructor The chapter demonstrates the reallocation behavior and forwarding of arguments to the constructor, emphasizing how variadic templates are used to forward arguments to the constructor, facilitating the in-place construction of objects within the vector class.']}], 'duration': 282.277, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC01457791.jpg', 'highlights': ['The chapter introduces the in-place back function using variadic templates, allowing for the construction of objects in place within the vector class, showcasing how this function eliminates the need to construct objects in the stack frame of main and then move them into the data storage of the vector.', 'The chapter explains the concept of Rvalue reference and move semantics, demonstrating its usage to minimize unnecessary copies and optimize memory allocation in a vector class by utilizing move semantics to transfer resources, reducing unnecessary copying.', 'Demonstration of reallocation behavior and forwarding of arguments to the constructor The chapter demonstrates the reallocation behavior and forwarding of arguments to the constructor, emphasizing how variadic templates are used to forward arguments to the constructor, facilitating the in-place construction of objects within the vector class.', 'The inefficiency of reallocation of heap memory during copy operations is discussed, emphasizing the need for optimization.', 'The importance of optimizing the pushback function to handle temporary variables is highlighted.']}, {'end': 2186.718, 'segs': [{'end': 1797.557, 'src': 'embed', 'start': 1761.201, 'weight': 1, 'content': [{'end': 1762.862, 'text': 'We have move, destroy, move, move, destroy.', 'start': 1761.201, 'duration': 1.661}, {'end': 1769.767, 'text': "The destroys are happening because we're actually releasing the old variables, the old vector three instances when we actually move them.", 'start': 1763.122, 'duration': 6.645}, {'end': 1775.511, 'text': "But otherwise, this is now looking pretty cool and we're constructing all of our memory in place, which of course is great.", 'start': 1770.047, 'duration': 5.464}, {'end': 1782.62, 'text': "Now, the last thing I'm going to show you in this extremely long video now is what happens when we have to destroy everything.", 'start': 1775.731, 'duration': 6.889}, {'end': 1787.826, 'text': 'Well, of course, in the destructor, we just need to make sure that we simply delete mdata.', 'start': 1783.04, 'duration': 4.786}, {'end': 1789.008, 'text': "It's as simple as that.", 'start': 1787.986, 'duration': 1.022}, {'end': 1797.557, 'text': 'Make sure you use the array deletion operator here to make sure that it both deletes the entire block of memory as well as actually cause the destructor of each element here.', 'start': 1789.088, 'duration': 8.469}], 'summary': 'Optimized memory management with in-place construction and array deletion.', 'duration': 36.356, 'max_score': 1761.201, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC01761201.jpg'}, {'end': 1814.666, 'src': 'embed', 'start': 1789.088, 'weight': 0, 'content': [{'end': 1797.557, 'text': 'Make sure you use the array deletion operator here to make sure that it both deletes the entire block of memory as well as actually cause the destructor of each element here.', 'start': 1789.088, 'duration': 8.469}, {'end': 1804.26, 'text': "Now, this in itself, by the way, is slightly difficult because at the moment we're not holding actual pointers.", 'start': 1797.777, 'duration': 6.483}, {'end': 1806.882, 'text': 'So if we were holding actual pointers,', 'start': 1804.46, 'duration': 2.422}, {'end': 1814.666, 'text': 'it would be important for you to actually delete each and every pointer there if you allocate memory every time you emplace back or push back.', 'start': 1806.882, 'duration': 7.784}], 'summary': 'Using array deletion operator ensures memory deletion and destructor activation.', 'duration': 25.578, 'max_score': 1789.088, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC01789088.jpg'}, {'end': 2018.604, 'src': 'heatmap', 'start': 1989.255, 'weight': 0.705, 'content': [{'end': 1992.077, 'text': 'And we can, of course, check that if we hit a five and take a look at this.', 'start': 1989.255, 'duration': 2.822}, {'end': 1995.599, 'text': 'F10 is doing a move destroy, as you can see.', 'start': 1992.417, 'duration': 3.182}, {'end': 2000.4, 'text': "Now, technically, if you're a keen observer, you may in fact deduce that.", 'start': 1995.839, 'duration': 4.561}, {'end': 2005.241, 'text': 'well, is that any better than this pushback rvalue reference function??', 'start': 2000.4, 'duration': 4.841}, {'end': 2009.642, 'text': "Because isn't it just doing a move at the end of the day anyway,", 'start': 2005.321, 'duration': 4.321}, {'end': 2016.124, 'text': 'even if it is in fact constructing the object in this stack frame instead of the main one? And you would be correct.', 'start': 2009.642, 'duration': 6.482}, {'end': 2018.604, 'text': 'How would you solve that? Leave a comment below.', 'start': 2016.444, 'duration': 2.16}], 'summary': 'Discussion on move semantics and object construction in c++.', 'duration': 29.349, 'max_score': 1989.255, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC01989255.jpg'}, {'end': 2079.05, 'src': 'embed', 'start': 2048.416, 'weight': 3, 'content': [{'end': 2053.199, 'text': 'Now, new might be scary here, because you might associate that with heap allocations, but if you use a placement new,', 'start': 2048.416, 'duration': 4.783}, {'end': 2059.203, 'text': "you're providing the actual memory for the object to be constructed in, which is obviously, coincidentally, exactly what we want here.", 'start': 2053.199, 'duration': 6.004}, {'end': 2064.945, 'text': "So what we can do is actually grab the memory address of this element, because this is where it's supposed to live,", 'start': 2059.362, 'duration': 5.583}, {'end': 2070.927, 'text': "and then simply forward these arguments into that constructor, not forgetting the actual type that we're trying to construct.", 'start': 2064.945, 'duration': 5.982}, {'end': 2073.007, 'text': 'So as simple as that.', 'start': 2071.167, 'duration': 1.84}, {'end': 2079.05, 'text': 'And by doing this, if we pop a breakpoint here and relaunch this, we have nothing in our console at the moment.', 'start': 2073.688, 'duration': 5.362}], 'summary': 'Using placement new, we can construct objects at specified memory addresses.', 'duration': 30.634, 'max_score': 2048.416, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC02048416.jpg'}, {'end': 2134.491, 'src': 'embed', 'start': 2104.488, 'weight': 4, 'content': [{'end': 2113.015, 'text': "Now, even though we've spent quite a long time writing this vector class here today and I hope that this was helpful to you there are still so many things that one would want to add to this class,", 'start': 2104.488, 'duration': 8.527}, {'end': 2115.657, 'text': 'such as probably the number one thing being arrays.', 'start': 2113.015, 'duration': 2.642}, {'end': 2119.16, 'text': "How do I remove elements from this? Well, you can't at this stage.", 'start': 2115.718, 'duration': 3.442}, {'end': 2124.865, 'text': "And that's why even though this reallocation function technically supports shrinking, we're not shrinking anywhere.", 'start': 2119.461, 'duration': 5.404}, {'end': 2130.969, 'text': 'And in addition to that, what if I want to in place an element or push an element into the middle of the vector?', 'start': 2125.085, 'duration': 5.884}, {'end': 2132.349, 'text': 'What do I do there?', 'start': 2131.289, 'duration': 1.06}, {'end': 2134.491, 'text': "That's also something that you would probably want to implement.", 'start': 2132.489, 'duration': 2.002}], 'summary': 'Discussion on adding functionality to the vector class including arrays, element removal, reallocation support, and inserting elements.', 'duration': 30.003, 'max_score': 2104.488, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC02104488.jpg'}], 'start': 1740.248, 'title': 'C++ memory management and vector implementation', 'summary': 'Covers managing memory and destructors in c++, including handling destruction and resizing vectors, with examples of move, destroy, and clear operations. additionally, it discusses the implementation of a c++ vector class, using placement new for constructing objects in place and potential future improvements.', 'chapters': [{'end': 2009.642, 'start': 1740.248, 'title': 'Managing memory and destructors', 'summary': 'Discusses managing memory and destructors in c++, including returning data, handling destruction, and resizing vectors, with examples of move, destroy, and clear operations.', 'duration': 269.394, 'highlights': ['The chapter discusses managing memory and destructors in C++ The discussion covers the fundamental concept of memory management and destructors in C++ programming.', 'including returning data, handling destruction, and resizing vectors The chapter includes examples of returning data, handling destruction, and resizing vectors, providing practical insights into memory management.', 'examples of move, destroy, and clear operations Various examples of move, destroy, and clear operations are illustrated, showcasing different scenarios of memory management and destructor usage.']}, {'end': 2186.718, 'start': 2009.642, 'title': 'C++ vector class implementation', 'summary': 'Discusses the implementation of a c++ vector class, covering the use of placement new for constructing objects in place and outlines potential future improvements such as adding support for arrays and iterators.', 'duration': 177.076, 'highlights': ['The use of placement new for constructing objects in place By using placement new, the actual memory for the object to be constructed is provided, avoiding heap allocation and allowing objects to be constructed in place.', 'Future improvements for the vector class, including support for arrays and iterators Potential future enhancements to the vector class are discussed, such as adding support for arrays, removing elements, pushing elements into the middle of the vector, and implementing iterators.', 'Limitations of manipulating elements in the middle of vectors due to memory contiguity The challenges of replacing and removing elements from the middle of vectors are highlighted, including the potential need for contiguous memory and the implications of triggering reallocation or shifting elements.']}], 'duration': 446.47, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC01740248.jpg', 'highlights': ['The chapter discusses managing memory and destructors in C++ The discussion covers the fundamental concept of memory management and destructors in C++ programming.', 'The chapter includes examples of returning data, handling destruction, and resizing vectors, providing practical insights into memory management.', 'Various examples of move, destroy, and clear operations are illustrated, showcasing different scenarios of memory management and destructor usage.', 'By using placement new, the actual memory for the object to be constructed is provided, avoiding heap allocation and allowing objects to be constructed in place.', 'Potential future enhancements to the vector class are discussed, such as adding support for arrays, removing elements, pushing elements into the middle of the vector, and implementing iterators.', 'The challenges of replacing and removing elements from the middle of vectors are highlighted, including the potential need for contiguous memory and the implications of triggering reallocation or shifting elements.']}, {'end': 2701.79, 'segs': [{'end': 2245.72, 'src': 'embed', 'start': 2187.178, 'weight': 1, 'content': [{'end': 2188.359, 'text': 'So I am going to address this now.', 'start': 2187.178, 'duration': 1.181}, {'end': 2193.463, 'text': 'The problem has to do with how we allocate and deallocate memory.', 'start': 2189.099, 'duration': 4.364}, {'end': 2200.17, 'text': 'Specifically, the problem lies in this delete call over here and also potentially in this delete call here.', 'start': 2193.684, 'duration': 6.486}, {'end': 2206.216, 'text': "As you know, we're manually calling the destructor in both the pop back and the clear function.", 'start': 2200.39, 'duration': 5.826}, {'end': 2211.06, 'text': "which is, you know, I mean a lot of care needs to be taken when you're manually calling the destructor,", 'start': 2206.656, 'duration': 4.404}, {'end': 2215.304, 'text': 'because the worst thing that can happen is the delete function also calls the destructor.', 'start': 2211.06, 'duration': 4.244}, {'end': 2219.889, 'text': "And for a type like vector three here, that's not really gonna do any harm.", 'start': 2215.765, 'duration': 4.124}, {'end': 2224.293, 'text': "However, let's make vector three a little bit more of a complex type.", 'start': 2220.329, 'duration': 3.964}, {'end': 2228.277, 'text': "Let's go ahead and just add some kind of, you know, memory block.", 'start': 2224.733, 'duration': 3.544}, {'end': 2230.537, 'text': 'into this Vector3 class.', 'start': 2228.777, 'duration': 1.76}, {'end': 2234.758, 'text': "I'll go down here into the constructor, in fact, into all of the constructors.", 'start': 2230.677, 'duration': 4.081}, {'end': 2239.179, 'text': "Now I'll just make it so that we allocate maybe like five integers or whatever on the heap.", 'start': 2235.198, 'duration': 3.981}, {'end': 2242.579, 'text': "So basically we've created a memory block that we now need to deallocate.", 'start': 2239.239, 'duration': 3.34}, {'end': 2245.72, 'text': "I'll add this exact code into everywhere else.", 'start': 2243.019, 'duration': 2.701}], 'summary': 'Addressing memory allocation issue in code, dealing with delete calls and manual destructor invocation.', 'duration': 58.542, 'max_score': 2187.178, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC02187178.jpg'}, {'end': 2303.552, 'src': 'embed', 'start': 2277.074, 'weight': 3, 'content': [{'end': 2282.398, 'text': "And, in fact, to simplify that and to make sure that is the case, I'm just gonna make sure that I actually delete both of them,", 'start': 2277.074, 'duration': 5.324}, {'end': 2285.24, 'text': "so that we're sure that we're never accidentally copying anything.", 'start': 2282.398, 'duration': 2.842}, {'end': 2290.723, 'text': "so now what we've got is a move constructor that properly moves that memory block and our general constructors.", 'start': 2285.76, 'duration': 4.963}, {'end': 2293.666, 'text': 'i forgot one here, the default one that allocates this memory block.', 'start': 2290.723, 'duration': 2.943}, {'end': 2298.249, 'text': "finally, we're going to de-allocate it, so delete it inside the destructor.", 'start': 2293.666, 'duration': 4.583}, {'end': 2301.151, 'text': 'this is the proper usage of this class.', 'start': 2298.249, 'duration': 2.902}, {'end': 2303.552, 'text': 'if it was to have a memory block, this is totally fine.', 'start': 2301.151, 'duration': 2.401}], 'summary': 'Demonstration of proper memory block allocation and deallocation in a move constructor and destructor.', 'duration': 26.478, 'max_score': 2277.074, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC02277074.jpg'}, {'end': 2428.469, 'src': 'heatmap', 'start': 2365.053, 'weight': 1, 'content': [{'end': 2371.999, 'text': "But when we start to actually play around with it a little bit, and especially with pop back, because we've called the destructor of vector three,", 'start': 2365.053, 'duration': 6.946}, {'end': 2376.743, 'text': 'which has in fact deleted this over here then what happens when we call this?', 'start': 2371.999, 'duration': 4.744}, {'end': 2378.884, 'text': "We're going to land in a world of hurt.", 'start': 2376.983, 'duration': 1.901}, {'end': 2382.887, 'text': 'So how do we get around this? Well, we still have to call the destructor here.', 'start': 2379.184, 'duration': 3.703}, {'end': 2384.128, 'text': "There's no real way around that.", 'start': 2382.927, 'duration': 1.201}, {'end': 2388.732, 'text': 'What we need to do instead is change the way that we allocate and deallocate memory.', 'start': 2384.589, 'duration': 4.143}, {'end': 2397.838, 'text': 'So specifically, vector destructor is going to call clear, which of course is going to go through everything and manually call the destructor.', 'start': 2389.292, 'duration': 8.546}, {'end': 2407.804, 'text': 'So now what we need to do is the new and delete operators that we use, we instead want to use versions that do not call the constructor or destructor.', 'start': 2398.218, 'duration': 9.586}, {'end': 2413.791, 'text': "So how do we do that? Well, keep in mind, there shouldn't be any constructors being called here at all anyway.", 'start': 2408.124, 'duration': 5.667}, {'end': 2416.775, 'text': "So there's no point at which we need to call a constructor.", 'start': 2413.931, 'duration': 2.844}, {'end': 2422.463, 'text': 'All we need to do here is essentially just malloc enough memory to fill up this new capacity.', 'start': 2417.216, 'duration': 5.247}, {'end': 2425.146, 'text': "So how do we do that? Well, we're not actually gonna use malloc because we're using C++.", 'start': 2422.523, 'duration': 2.623}, {'end': 2428.469, 'text': "Instead, we're going to use operator new.", 'start': 2426.548, 'duration': 1.921}], 'summary': 'Changing memory allocation to avoid calling constructors and destructors in c++.', 'duration': 63.416, 'max_score': 2365.053, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC02365053.jpg'}, {'end': 2428.469, 'src': 'embed', 'start': 2384.589, 'weight': 0, 'content': [{'end': 2388.732, 'text': 'What we need to do instead is change the way that we allocate and deallocate memory.', 'start': 2384.589, 'duration': 4.143}, {'end': 2397.838, 'text': 'So specifically, vector destructor is going to call clear, which of course is going to go through everything and manually call the destructor.', 'start': 2389.292, 'duration': 8.546}, {'end': 2407.804, 'text': 'So now what we need to do is the new and delete operators that we use, we instead want to use versions that do not call the constructor or destructor.', 'start': 2398.218, 'duration': 9.586}, {'end': 2413.791, 'text': "So how do we do that? Well, keep in mind, there shouldn't be any constructors being called here at all anyway.", 'start': 2408.124, 'duration': 5.667}, {'end': 2416.775, 'text': "So there's no point at which we need to call a constructor.", 'start': 2413.931, 'duration': 2.844}, {'end': 2422.463, 'text': 'All we need to do here is essentially just malloc enough memory to fill up this new capacity.', 'start': 2417.216, 'duration': 5.247}, {'end': 2425.146, 'text': "So how do we do that? Well, we're not actually gonna use malloc because we're using C++.", 'start': 2422.523, 'duration': 2.623}, {'end': 2428.469, 'text': "Instead, we're going to use operator new.", 'start': 2426.548, 'duration': 1.921}], 'summary': 'Change memory allocation to use operator new in c++.', 'duration': 43.88, 'max_score': 2384.589, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC02384589.jpg'}, {'end': 2563.621, 'src': 'heatmap', 'start': 2506.553, 'weight': 0.809, 'content': [{'end': 2510.114, 'text': 'So to make this a little bit more simple, we could just call clear like that.', 'start': 2506.553, 'duration': 3.561}, {'end': 2512.795, 'text': 'Everything is safe in the new block anyway.', 'start': 2510.574, 'duration': 2.221}, {'end': 2516.077, 'text': "And now that we've done that, we of course reassign data and new capacity.", 'start': 2512.955, 'duration': 3.122}, {'end': 2519.378, 'text': 'Only thing that the clear has actually done is also set our size to zero.', 'start': 2516.277, 'duration': 3.101}, {'end': 2521.024, 'text': 'which is slightly problematic.', 'start': 2519.798, 'duration': 1.226}, {'end': 2522.29, 'text': "I'm actually gonna go back on that.", 'start': 2521.104, 'duration': 1.186}, {'end': 2527.341, 'text': "I'm actually just gonna copy this and replace it with clear here, just that we leave our size intact.", 'start': 2522.35, 'duration': 4.991}, {'end': 2530.943, 'text': 'And that should be the end of this new realloc function.', 'start': 2527.521, 'duration': 3.422}, {'end': 2533.944, 'text': 'And finally, of course, with this delete up here,', 'start': 2531.003, 'duration': 2.941}, {'end': 2541.668, 'text': "instead of doing that kind of array delete operator there we're going to call operator delete mdata and then mcapacity times size of t.", 'start': 2533.944, 'duration': 7.724}, {'end': 2543.309, 'text': 'This will not call any destructors.', 'start': 2541.668, 'duration': 1.641}, {'end': 2546.991, 'text': "Clear is what we'll go through and call all of the destructors.", 'start': 2543.709, 'duration': 3.282}, {'end': 2552.734, 'text': "So now if we go back to our original scenario, let's hit F5 and hopefully we won't get any crashes.", 'start': 2547.411, 'duration': 5.323}, {'end': 2556.636, 'text': 'And you can see our program runs successfully, no crashes whatsoever.', 'start': 2553.214, 'duration': 3.422}, {'end': 2559.218, 'text': "We're still firing the destructor wherever we need.", 'start': 2556.657, 'duration': 2.561}, {'end': 2563.621, 'text': 'And that should emphasis on should be bug free.', 'start': 2559.358, 'duration': 4.263}], 'summary': 'Modifying the realloc function to address size issue and prevent crashes.', 'duration': 57.068, 'max_score': 2506.553, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC02506553.jpg'}], 'start': 2187.178, 'title': 'Memory management in vector class', 'summary': 'Addresses memory allocation and deallocation issues in the vector3 class, emphasizing the implementation of constructors, move constructor, move assignment operator, and destructor to prevent memory leaks and crashes, with a focus on operator new and operator delete for bug-free operation.', 'chapters': [{'end': 2224.293, 'start': 2187.178, 'title': 'Memory deallocation issue', 'summary': 'Addresses the issue of memory allocation and deallocation, particularly with the manual destructor call in the pop back and clear functions, highlighting potential problems and the importance of careful memory management.', 'duration': 37.115, 'highlights': ['The issue lies in the manual destructor call in the pop back and clear functions, requiring careful memory management. Manual destructor calls in multiple functions', 'Potential harm from the delete function calling the destructor for complex types like vector three. Impact on complex types']}, {'end': 2364.633, 'start': 2224.733, 'title': 'Memory block management in vector3 class', 'summary': 'Explains the proper allocation and deallocation of a memory block in the vector3 class, including the implementation of constructors, move constructor, move assignment operator, and destructor, to avoid memory leaks and crashes.', 'duration': 139.9, 'highlights': ['The chapter explains the proper allocation and deallocation of a memory block in the Vector3 class, including the implementation of constructors, move constructor, move assignment operator, and destructor, to avoid memory leaks and crashes. Proper memory management in Vector3 class, implementation of constructors, move constructor, move assignment operator, and destructor, to avoid memory leaks and crashes.', 'The move constructor properly moves the memory block, and the general constructors initialize the memory block. Proper implementation of move constructor and initialization of memory block in general constructors.', 'The chapter demonstrates the potential issue of trying to delete a memory block that has already been deleted, resulting in a crash. Demonstration of potential issue when trying to delete a memory block that has already been deleted, leading to a crash.']}, {'end': 2701.79, 'start': 2365.053, 'title': 'Implementing memory allocation and deallocation', 'summary': 'Discusses the need to change memory allocation and deallocation in a vector class, demonstrating the use of operator new for memory allocation and operator delete for memory deallocation, ensuring proper destructor calls and bug-free operation.', 'duration': 336.737, 'highlights': ['The need to change memory allocation and deallocation in a vector class The chapter emphasizes the necessity to alter the way memory is allocated and deallocated in a vector class to ensure proper operation.', 'Demonstration of using operator new and operator delete for memory allocation and deallocation The chapter provides a demonstration of using operator new for memory allocation and operator delete for memory deallocation, enabling the allocation and deallocation of memory without calling the constructor or destructor.', 'Ensuring proper destructor calls and bug-free operation The chapter highlights the importance of ensuring proper destructor calls and bug-free operation in the vector class by using clear to call all destructors and implementing a new realloc function.']}], 'duration': 514.612, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/ryRf4Jh_YC0/pics/ryRf4Jh_YC02187178.jpg', 'highlights': ['Demonstration of using operator new and operator delete for memory allocation and deallocation', 'Proper memory management in Vector3 class, implementation of constructors, move constructor, move assignment operator, and destructor, to avoid memory leaks and crashes', 'The need to change memory allocation and deallocation in a vector class', 'The move constructor properly moves the memory block, and the general constructors initialize the memory block', 'The issue lies in the manual destructor call in the pop back and clear functions, requiring careful memory management', 'Potential harm from the delete function calling the destructor for complex types like vector three']}], 'highlights': ['The chapter introduces the in-place back function using variadic templates, allowing for the construction of objects in place within the vector class, showcasing how this function eliminates the need to construct objects in the stack frame of main and then move them into the data storage of the vector.', 'The STD vector class is a resizable array with heap-allocated data, providing dynamic and runtime capabilities.', 'The pushback function involves checking if the current size exceeds the capacity and reallocating with a new capacity of 1.5 times the original, effectively growing by 50% to support dynamic resizing.', 'The chapter discusses managing memory and destructors in C++ The discussion covers the fundamental concept of memory management and destructors in C++ programming.', 'The chapter focuses on replicating the behavior of the STD vector class in C++, providing a deeper understanding of data structures and the vector class functionality.']}