Memory Allocator Errors and XMalloc

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today i want to talk about xmalic and handling memory allocation errors hey welcome back everybody today i want to talk about memory errors or basically the errors that come back from your memory allocator i've got a bunch of ideas i want to talk about relating to memory allocators in the future be sure to let me know down in the comments if that's a topic that you want to hear more about also a big thanks to all of you who support this channel with likes and by subscribing and by contributing through patreon where just in case you're new that's where you can get access to all the source code for my videos and access to my monthly virtual office hour i definitely couldn't do what i do without you so thanks for all the help so today's video is inspired by a question that i get off and on and that is what is x malek so when starting out with c and c plus plus people usually come across malek and its siblings calc realic and of course free these are your standard memory allocation functions in c and if you're thinking well i use c plus plus and i've never seen malik don't worry if you've used new and delete well you're probably calling malik it's just under the hood so we're all familiar with malik but then some of you are going to be wandering along looking at some open source project or checking out some library and you're going to come across xmalic or maybe you get some error message complaining about an xmalic failure and you're getting confused and yes x malik is a thing but it's a little tricky because it's not a standard thing for example if i come down here and i type in man x malek well i'm not finding anything i don't see any results and so even though i haven't yet really told you what it is we already have a drawback right x malik is common enough it does show up in libraries here and there and people sometimes write their own version of it i have myself at times and every time i've seen x malek it's more or less the same so what is it well let's start with mallet so malik is gonna look if we look at malik it's gonna look something like this right here it takes a size basically the number of bytes of memory that i need and then it's going to return a pointer to a block of memory that is at least that size that is assuming it's successful now if it's not successful malik is going to return null and that's how we know if something went wrong but the thing is is that malik almost never fails it's typically only going to fail if you try to allocate a ton of memory which we looked at in a previous video so check that out if you want to see more about that but usually that's the consequence of a program bug an unusually large input or something really strange going on in your system but the point is it's really rare for most programs so for good or ill let's get out of this for good or ill usually we see or often we see code that looks like this and this includes some of my own example code now i usually do this because it's a demo and i want to keep things readable but the point is is that if malik were to fail in this code you can see i'm calling it twice here to allocate integers if it were to fail then one of these pointers would be null and then down here when i actually try to write to that memory i'm probably going to segfault now i could have you take my word for it but let's not do that let's actually run it so we can compile it and run it and so you can see that okay you can see that this this works the way it should work but i want to see what happens when it fails so let's just come up here and write our own malik function now some of you may not realize that you can do this but i can just make a function that looks just like malik and this will replace the system's version of malloc okay and let's just say return null okay not a very useful allocator but it is useful for showing us what happens if malik returns null because that's all this malik is going to do now note that if i didn't want to actually add code to my existing project i could test this using a shim if you're not familiar with shims i have another video you should check out definitely i'll put a link down in the description but a shim would allow us to inject errors without actually changing our program code so that's nice but for today this is going to work to show us what happens when our allocator fails and if we compile it and we run it you can now see that we do in fact fault so that's a relief i guess i mean it's never good to see a seg fault but it's good that the universe makes sense now the problem here in my code is that i'm not ever checking my return values right i'm just assuming that malik is gonna succeed so if it fails we're just basically releasing this null pointer into our code somewhere and we're almost always going to get some really ugly crashes no nice error messages no cleaning up no saving files we just segfault and crash and that's not very satisfying it's definitely not good i mean it's okay for little demos but if this were production software this wouldn't be good now one straightforward solution we could do is just to add an if statement here after each of these say if p1 is null then let's print out an error saying error memory exhausted something like this and then let's just exit and with exit failure okay and then let's do the same thing down here with p2 okay so now we're checking these error messages and now if we come down and compile it now if i run it you can see now i do get a clean error message i actually get to handle these errors and so this is nice but it does make my code a little messier i mean for this really simple program i've just added a bunch of extra code it's also pretty repetitive and so we could refactor things let's come up here and like we could make a handler function something like error and exit and we could just take this code from here and move it up in here and then we can just come down and call error and exit in both of our functions here so that refactored version is a little cleaner but it's still going to be pretty annoying if i do this throughout my entire program i mean in this case it's not a big deal because it's a tiny program with only two mallet calls but if i had a lot of code with a lot of memory allocations this could get pretty annoying it should feel pretty tedious and it's also super easy to forget as i'm going along to forget to add one of these if statements every single time so that's where xmallet comes in now like i said before xmalic is not a standard function and it can be a bit controversial some people love it others think it's the worst thing ever i'm going to leave that up to you be sure to let me know down in the comments i'm sure you will anyway how you feel about x malik but the main idea with x malik is that it's a version of malik that never returns null okay so the assumption is and it's a pretty good assumption is that malek returning null is a fatal error super rare but it's a fatal error and your program is probably not going to be able to keep working after that and so because the program has basically hit a fatal error what's the point of returning let's just exit the program now i've seen a few different versions of x malek show up in different projects the simplest is gonna look something like this let's just make something that's gonna look just like malik we're gonna call it x malik and then here what we're gonna do here is just make a pointer a temporary result pointer that's going to let's call malik and that's the temporary result is going to store the result from malik and then we're going to return that result eventually but before we do we're just going to come in here and say if result is null then let's call error and exit and i'm gonna move this up here and of course i don't have to call error and exit i could put whatever i want in here but this is a really simple version of xmalic and i've seen something like this show up in a bunch of different projects a bunch of different libraries and now once we have this we can come down here convert all of our mallet calls to xmalic and we can just remove the if statements because we no longer have to check to see what xmalik returns because we know that it's never going to return null it's only going to return if it's successful now this is nice because my code is now cleaner and if i ever run into a memory issue at least i know my code will fail in a consistent way i don't have to remember to check my returns from malik each time but like i said it's a little controversial so what's the downside well the most common complaint that i've heard over the years is that x-malik at least in this form doesn't give you much flexibility it doesn't let you actually do much recovery it does avoid the case where a null pointer just gets shot back floating randomly around your code and we'll just see what happens but it does just exit and so you don't have an opportunity to do much cleanup now often we don't care but say i'm writing a word processor or an image editor if this happens i probably want to at least try to save the current document before i crash so here we have a few different options some implementations that i've seen allow the user to specify an error handler function where like i just have this error and exit you could allow someone to supply you a function either through an extern function pointer or through a separate function where i like register a callback and say hey call this error handler when this error happens and then xmalic will automatically call that function anytime that it fails so this is basically the same as what i'm doing here but it just gives you the ability to customize a few things so that gives you a little more flexibility i could also add another argument to x malek over here i could actually add a function pointer out here i'm not going to do that today because it's going to make this a little messy but i could i could add a function pointer as a separate argument to x malloc so that each call to x mallet could specify its own cleanup function that's a little more flexible but it's also a little messier especially if most the time you just want to exit and adding more arguments to xmalic basically makes x-malik no longer just a drop-in replacement for malloc and so for some people that might also be a drawback now one more thing you want to keep in mind about this error handler function is you have to be really careful here because when it's called we have to assume that we can't get any new memory that means we're basically assuming that malik is broken we can't actually get anything from malik so whatever cleanup code we put in here better not need to allocate memory if it does then well we're back to where we started with an ugly segfault so we need to be really careful about what we put into our clean up code but i hope that helps you in your next project like this video if it was helpful subscribe so you don't miss the next one check us out on patreon thankful and until next week i will see you later
Info
Channel: Jacob Sorber
Views: 9,541
Rating: undefined out of 5
Keywords: memory allocator errors, memory allocation error, exhausted memory, malloc, xmalloc, xmalloc errors, malloc memory errors, malloc returns NULL, can't allocate memory, programming memory errors, malloc vs xmalloc, memory allocator, allocator xmalloc, calloc, realloc, free, Catching memory allocator errors, programming video, c language, c/c++, programming tutorial, example code
Id: dpjt_D8xcPU
Channel Id: undefined
Length: 9min 47sec (587 seconds)
Published: Tue Nov 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.