Mastering Memory: Allocation Techniques in C, C++, and ARM Assembly

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone thanks for watching Lori wired and in this video we're going to learn how to dynamically allocate and deallocate memory in C++ C and raw assembly now dynamic memory is really important if you're not aware of the amount of memory that you need at compile time of the application so this allocates additional memory on the Heap for you to use if you need to allocate additional memory while the application is running and executing one really good example use case of this is if you have an array and you're not aware of how many values that you're going to need since you're going to be setting these values at runtime of the application so maybe you need to allocate additional space to store extra values for your array or maybe you need to resize the array and give some memory back over to the system to use so let's get right into it and let's see what it looks like now I'm going to be running this on a Raspberry Pi and I already have a couple folders and a couple stub appc applications that are going to be in C++ C and then arm V7 assembly so we're going to begin with C++ here and this is going to be the highest level view of memory allocation and deallocation this is going to be the most simple if you're trying to work inside of this language then we're going to move on to C which is going to be a little bit lower level and just a little bit more challenging to implement before we move on to our last language in armv7 assembly which is going to be the most challenging and the most complicated to implement but it's going to allow us to get the lowest level and closest to the underlying instructions for the operating system so let's first take a look at our C++ program so let's move over to our C++ folder and here is the stub program that has just a little bit of a C++ application to get us started so let's go ahead and edit this now I already have a TR catch statement in here that's going to handle if we have any errors while we're trying to allocate this now there's two keywords that we need to keep in mind while we're working in C++ these two keywords are going to be new and delete so the new keyword is going to allocate a little bit of space and a little bit of memory to hold our Dynamic values and then the delete keyword is going to free up that memory once we're done with that and allocate it back over to the operating system to be in control of so let's create a one Dynamic integer value and allocate that memory and then go on and go ahead and delete that once we're finished with that so let's go ahead and add some code and we're going to allocate space for one additional Dynamic integer so I'm going to declare that up here so let's just insert that and this is going to be of type in Star since this is a pointer to a dynamic integer so let's do in Star as our data type and let's just call this num pointer and we're going to set this equal to null pointer as kind of a default value now inside of our Tri catch statement let's remember those keywords that we wanted to use so the new keyword remember is going to be responsible for actually allocating the space to store our Dynamic integer so let's add that inside of our Tri catch statement just in case we run into any errors while we're allocating this memory so I'm going to add a new space in here let's do num pointer equals new int so now we have successfully allocated the memory to store that Dynamic integer so now we can just go ahead and set that value so let's set our value to num pointer and we'll dreference that with our star over here and we can do whatever integer we want let's just pick the number 42 so now we've successfully allocated that memory and set that value to our integer value 42 now I just want to go ahead and print that out to the screen so let's do c out and we'll call this Dynamic number and then we'll print out that deeren num pointer so this is going to print the value 42 so let's just end it with a new line now this is going to print that value to the screen but remember now that we've allocated that memory we are responsible for deallocating that memory so let's use that delete keyword to free up this memory now that we're actually finished with it and we've printed that value to the screen so let's go down here and all we need to do is delete num pointer so this is giving that memory back to the operating system to be responsible for in case any other applications are needing to allocate or deallocate memory or maybe the system itself needs additional memory so that's all we need to do for C++ it's not too challenging so let's just run this and see what happens now I'm going to compile my application and generate my executable looks good no errors so now we have this additional allocate executable and if we run this here we go Dynamic number is 42 now this dynamically allocated additional space and G gave us a pointer to an integer value that we could use then we set that value to 42 in memory and then we went ahead and deleted that memory since we were finished with it after we printed that value to the console now let's move on to our C program and so this is going to be a little bit lower level than the C++ program that we were working on let's see what that looks like inside of here let's move on to our Alec C and let's start working on the stub that I've already created now we have our simple application which is basically doing nothing except returning success at the moment so let's go ahead and add our code and this is going to be equivalent to what we wrote in the C++ application but we're going to be working on this in the C version of this so let's create our new pointer again it's still going to be of type instar and let's call this num pointer but now we have two different keywords we're going to need to be looking at now we're going to be working with Malik and free so now Malik is going to be the keyword we're working on in C that is going to be responsible for allocating some dynamic memory and then free is going to free up that memory once we're done with it now the difference between Malik as well as the new keyword in C++ is that the new keyword is a little bit more complex and it goes ahead and auto initializes an object instead of Malik and C which doesn't do any kind of initialization so let's create our new keyword so this is going to be Malo and then we also have to pass the size of the data that we want to use so we have this really convenient size of keyword and then we're going to pass an integer value of that so it knows how much memory it needs to allocate for our program so let's do size of int so that's going to allocate new memory and the size of an integer that we're going to be able to use afterwards now one more thing is we're going to have to type typ cast this to a pointer to an integer so that same data type in Star is going to typ cast this so that we can put that pointer value inside of our nonp pointer variable now we've successfully allocated that memory so let's do a quick check to make sure this memory allocation was successful we don't have a null pointer before we go ahead and set the value of this Dynamic variable so let's just do a quick if statement so if num pointer equals null so that means our allocation was not successful we're just going to go ahead and return one so that's kind of meaning an unsuccess of the application otherwise we can go ahead and set our value so really similar to C++ syntactically here we can just use star nump pointer to D reference that and set the value at that location and we'll just set that equal to 42 which is the same integer value that we used previously now remember we have to use the free keyword to free that memory once we're done so let's go ahead and print that value to the screen and then free that up so I'm going to do print F and we'll just do Dynamic number and we'll print that integer value and then the new line and then add our dereferenced pointer here so this is just going to print the same thing to the screen just like we did in C+ plus so now let's go ahead and free num pointer so we have allocated our new memory made sure that we allocated successfully set our value printed it to the console and then free that memory back to the operating system once we were done with it so let's compile and run this let me do GCC allocate do C- o and then generate our execute looks like I've got a little bit of extra space in here there we go so now we have our new executable let's make sure that that prints the same thing and successfully runs just like the C++ program very nice so Dynamic number is 42 that was the next stage of our allocation and deallocation so now let's move on to our assembly implementation inside of armv7 assembly and this is going to be the lowest level implementation that gives us the most responsibilities to worry about while we're implementing this process now we can use Malik just like we did in the C application and call that but this is inside of the C standard Library so let's instead use map and M unmap as our two keywords to be responsible for while we're dynamically allocating and deallocating this memory now map allows you to map a file or a device into memory but it does allow you to map memory with an anonymous file if you specify the flags so this effectively lets us do the same thing so let's go into our file and let's move on to Alec assembly and here is our stub application that we're going to be working inside of and let's just edit our allocate dos now I already have a program kind of set up that's going to be responsible for deallocating the memory for us so let's go ahead and call our map CIS call now making this system call directly allows us to invoke the operating system instead of using the C standard Library call to Malik from inside of our assembly application so let's go ahead and add some code I have this function defined up here that already has our flags set so all we need to do is make this system call so that it sees what flags we're wanting to set all these flags are going to let us know that we want to let the kernel choose the memory address for the dynamic memory that we're allocating as well as set the number of B that we're wanting to allocate in this example one page so it also lets us set the protections for the memory if we want to be able to make it readable writable Etc now let's go ahead and make our system call so let's create a new line in here now we have to look up the system call number since we're calling this directly so we can go to the chromium OS docs go down to our 32-bit version of arm since we're working in arm V7 assembly and then all we have to do is find the map call which defines the CIS call number now the CIS call number is going to be stored inside of our R7 register inside of rv7 assembly so all we need to do is do move R7 and then the immediate value defined here 192 and then this is going to let the system know what system call we're actually trying to invoke and then we can use our system call instruction to actually invoke that M map CIS call so this is going to pass all of these flags that we defined up here already let the operating system know we want to call mmap and then actually invoke that so now the address is going to be stored inside of our r0o register to the newly allocated memory that we're trying to point to now if the allocation was unsuccessful we're going to get the value -1 so why don't we compare the value currently in r0 this is going to be the memory address of the new location where we're allocating our memory and let's see if that is the value -1 and if it is the value1 we're going to Branch equal and let's jump over to our label aloc failed let me just add my hash sign right here so we're comparing the immediate value neg 1 otherwise we should be good to go and go ahead and use this memory location to store our Dynamic integer so why don't we put the value 42 inside of this memory location that we have dynamically allocated so I'm going to pick on our R1 register since we're going to use this to store the value inside of that memory location so let's move the immediate value 42 temporarily into our R1 register so now that we've done that we're able to store that value into the memory location that we have dynamically allocated so let's see use our store register instruction and we're going to store that R1 value into the memory address that is pointed to by the r0o register that was returned to us when we invoked the M map sis call up here now that we've done that successfully and made sure we've properly allocated our memory we can jump over and Branch to the Alec exit so this is going to skip over the Alec failed label and jump right over and pop us out of this current function now inside of assembly we are also responsible for deallocating the memory that we're looking at so if you look down here I've already added the system call to deallocate the memory so down here we're calling the M unmap CIS call which is the immediate value 215 which is going to let the operating system know that this is the system call that we're trying to make and then we pass in the value that is that was returned to us in the previous function which is going to be inside of our r0o register so that is the current memory address that we dynamic Al allocated that memory inside of now we have successfully called the map CIS call to allocate additional memory for us made sure that we had a legitimate pointer and the allocation did not fail we set that value to the immediate value 41 and we go ahead and call printf to print that value to the screen and we make sure that we cover our tracks and deallocate that memory by invoking the M unmap thisis call once we are finished with that value so let's go ahead and run that application and see what happens so let's do arm Linux g e ABI assemble we're going to take our allocate dos and generate our object binary looks good no mistakes in here and then let's create our executable do arm Linux G eabi GCC and I'm going to statically link that so we can get our print F value which is inside of our C standard library and we'll take our allocate oob binary and generate our executable so now all we have to do is run our allocate and make sure that we printed this successfully and there we go Dynamic number is 42 so now we have successfully completed our allocation in raw armv7 assembly where we allocated additional memory using the M map system call we set that value to the immediate value 42 and then once we were finished with that value we called the M on map system call so that we could free that back to the operating system so thanks so much for watching Lori wired everyone and I'll catch you in the next [Music] video I have no boost left I'm not going to hit this car oh my gosh truck out of the way we have [Music] Boost
Info
Channel: LaurieWired
Views: 138,134
Rating: undefined out of 5
Keywords:
Id: HlUBE70h2C0
Channel Id: undefined
Length: 17min 5sec (1025 seconds)
Published: Wed Feb 28 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.