A memory leak occurs when dynamically allocated memory (using malloc) is not properly freed with free(), leaving allocated memory permanently reserved even after the program no longer needs it; this typically happens when allocation and deallocation occur in separate parts of the code without proper coordination, such as returning dynamically allocated arrays from functions without ensuring the caller knows to free them, or modifying pointers so they no longer point to the original allocated address.
Memory Leaks in C/C++: Causes, Detection, and Prevention Strategies
Added:in today's video I want to discuss what are actually memory leaks and when do they happen so first things first without memory leaks well you know that in --see you have a lot of options when it comes to declaring your own variables you can have statically allocated variables can have globally allocated variables and you can help dynamically allocated variables so the statically wants out stuff like just int a right course file you don't have to so you allocate that memory for yourself for your own program and you use it whatever doesn't matter once the function finishes its execution it's going to be the allocated because it's been allocated on the stack simple enough then there's the globally allocated memory which well if you have it here for example it's going to be globally allocated and it's going to be D allocated when the program finishes execution right so that's also very simple and you also want this global variable to be always act accessible right so it makes sense to be removed only after the program has stopped completely from running now if let's try a dynamically allocated in so you can say int and gonna say a pointer a equals M a lock size of in so that is a dynamically allocated variable right we have a and we can give it a value let's say five and we can even print it right you say % d backslash N and then dereference a right because a is a pointer so if i run this you'll notice i get 500 solid this worked the memory is not on the stack nor is it global it's on the heap right now what's the issue here the issue is this is dynamically allocated right this guy is dynamically allocated it's memory is dynamically allocated and it's not managed by c automatically so whenever you're declaring such things you you actually have the responsibility of freeing memory right so we know that whenever you encounter an Emma look or see a lock or whatever you should deallocate that piece of memory using three right just three a and that's it whenever you are done with using it right it makes sense and in all actuality it's a very simple rule and nobody should actually have problems following this right well we can take a look at an example and see why this is very dangerous if not used properly so up here I have defined a function that well takes in an integral and what it does is allocate a dynamically allocated array using analog and it simply assigns that array values between 0 and 100 right so I just have this Rand call and I just module that 100 so I'm going to get only values from 0 to 99 inclusive right so now if we try to use this function simple enough I just gonna have to call random array of let's say 15 I just want 15 numbers and I'm gonna store it inside our pointer here just an array pointer okay and I'm gonna iterate over it and print all the values to see that it works okay so now I have that and if I try to run this and I just I get proper values from 0 through 99 right so everything seems to be working perfectly now what's the issue with this code you might notice that I said that this guy returns dynamically allocated memory why that's because well it returns an array and when you're trying to return an array you can't just take the whole array and copy it to the place you're calling it you have to well allocate it either globally or on the heap if you were to for example instead of having it dynamically rket and just statically allocated say int array of n right then whenever you're returning this array is going to be de-allocated so that memory is already gone so you cannot really use it after actually assigning it right so imagine this you go so we're gonna call this guy here random array and it's going to allocate this on the stack of these functions call and then it does this job by populating it and then it returns now after it returns it D allocate everything on the stack and so it happens that this array is actually on the stack so we are returning a pointer that is then the allocated which is not good that is why we are doing the dynamically allocated pointer right that's going to work if you actually pass it in here it's not going to get the allocated because it's dynamic we we have to be allocated oh that issue we have to delegate it but how do we actually know sure so okay we get a dynamically allocated memory so we have to know here that oh this is them calculated so gonna have to free eat once we're done with it so you can say free array as you can see here I have created without actually freeing I have created a memory leak this is what it's called a memory leak and it is kind of hard to trace as you can see so as you can see here the Emma log and the free calls our own separate functions and that's what that's where memory leaks usually occur this is when you're allocating memory in one place of the program and you have to deallocate it somewhere else with da know exactly where right so how can we actually fix this and fix this memory leak in now is it's very simple because instead of allocating it here dynamically there's one more way and that is to not return an array and actually pass in the arrays of parameter right so if i say here void and passing here the array say int array int pointer array right i don't have to allocate it here so this just kind of get out of the picture and we just have to pass in our array from the corners perspective and now it's kind of simple now I can decide okay well we say int array in pointer array equals I can actually Emma look this size of int times 15 let's say and then I can pass in this array I just say array and 15 cool and now I can see there's a problem with my program that it doesn't have a free call even though I have an Emma lock on this function so I'm going to have to add it here after I'm done with it see so now I kind of coupled the Emma lock call with the free call and it also not going to make sense that we have to deallocate this but the really cool thing about this is you don't even need dynamically allocated memory here you can just say just as easily just say int array of 15 and it and allocated on the stack right and now we don't have to deallocate it because it's on the stack it's not on a heap some global and this guy is going to work fine and dandy without any problems because all it's doing is accessing an array that is that happens to be allocated on the stack right so this is what memory leaks are they are just basically am a lock calls or see a lot course or any allocation calls that don't have a pair free call right so you have to be careful whenever you're dealing with these types of things there are some other really strange cases where for example let's get back to that dynamically allocated array where instead of doing just that you also use the array to iterate over itself really so instead of saying this array of I you say just array plus plus and then you dereference this for example so if we try to run this you'll notice that it printed all the numbers correctly the issue is and design of code right this array this basically this memory address has been changed right it's no longer pointing to the start of the address you've allocated right so whenever you are trying to free this this is no longer the same as dibs because it's been modified and memory leaks and crashes do happen like that it might sometimes this type of free call might actually work who knows maybe somebody has done something like array equals null let's also valid I just kind of make it a null pointer and we're calling free on that it's not going to crash it's going to work properly but this didn't free anything and this array is still allocated even after finishing the functions execution right so there are a few things to take from this video basically try as much as possible to pair the Emma log and a free call on the same function right if need if need be if you need a piece of memory everywhere in your program I suggest to just use global nobody allocated memory it's much easier to work with you don't have to deal with the allocation and reallocation whatnot and don't modify the actual pointer if you really want to modify you can make a copy of it for example you can say int array copy equals array and then you can actually change this for guys right you can say array copy and it's going to be fine right and it's not going to break and it's going to be de-allocated after you've finished doing whatever you started doing with the array so I hope this has helped you a little in understanding what exactly is a memory leak in C for any questions do leave them down the comments below and well thank you guys so much for watching take care
Up Next

Parsing C/C++ with libclang and Python: A GSFC Dev Group Talk
@andreyoung4442
1.5K views•2018-08-31

BitTorrent Protocol Explained: Piece Selection & Peer Choking
@StevenGordonAU
481 views•2013-02-22

C Struct Serialization to Files in C Programming
@CodeVault
81.8K views•2020-04-21

Enigma Machine Mechanics: WWII Encryption Explained
@JaredOwen
13.2M views•2021-12-11
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Computer Science











![#20. Операторы new / delete и new [] / delete [] | Язык С++ для начинающих](https://i.ytimg.com/vi/ECeoy6VjV4w/maxresdefault.jpg)

![C++ Tutorial [046] - malloc und free GERMAN](https://i.ytimg.com/vi/6GP2xIhlwnY/maxresdefault.jpg)

























