+ 12

What is memory leak? can anyone explain with example

27th Sep 2019, 2:38 PM
Ram Rabari
Ram Rabari - avatar
13 Answers
+ 14
It's when your program starts eating up more and more memory it doesn't need. It happens when you request new memory but never free unused memory. Simple C++ example: cheese slice_cheese(cheese*& c){ if(c->empty()) c = new cheese; return c->slice(); } In the `if` we leak memory because we never free the old `cheese` when making a new one. The old cheese will take up space but we no longer have a pointer to it so that block of memory is completely unusable. If you call `slice_cheese` over and over you will start leaking a lot of memory.
27th Sep 2019, 2:55 PM
Schindlabua
Schindlabua - avatar
+ 13
When you create something dynamically / allocate memory on heap but then forget to delete/free that allocated memory (de-allocation) after using it, it's called memory leak. int main() { int var = new int(5); cout << var << endl; } // Here is memory leak as I didn't delete the memory from heap after using. delete var; // It will delete the memory.
27th Sep 2019, 2:54 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 8
Ipang It's a reference to a pointer :P It's a minor detail but if the function takes merely a pointer and you change that pointer in the function then that change will not be reflected outside of the function. Because like with all things, the pointer you pass in will be copied. Just like how void foo(int a) { a = 4; } does nothing and void foo(int& a){ a = 4; } does. In this case I want to assign to the pointer so I have to pass the pointer by reference! `cheese*&` is not very common but I wanted to find a small example. Memory leaks often happen when passing around pointers between functions.
27th Sep 2019, 3:50 PM
Schindlabua
Schindlabua - avatar
+ 7
This is one reason why languages like Java and C# with automatic garbage collectors have become popular lately, to free up the programmer from the responsibility of having to keep track of all the dynamically allocated memory.
28th Sep 2019, 11:24 PM
Sonic
Sonic - avatar
+ 5
Yeah but C++ also introduced smart pointers which do all the deleting/freeing stuff for us automatically.
29th Sep 2019, 10:21 AM
blACk sh4d0w
blACk sh4d0w - avatar
+ 4
Schindlabua What is that `cheese*& c` is it a reference, a pointer? this is the first time I see them mixed together. Enlightenment please?
27th Sep 2019, 3:36 PM
Ipang
27th Sep 2019, 4:23 PM
Schindlabua
Schindlabua - avatar
+ 3
Aah Big Thanks Schindlabua Let me try to read it first for now. Might drop a comment there if I need help with something (hope not) ✌
27th Sep 2019, 4:27 PM
Ipang
+ 1
Schindlabua "It's a minor detail ..." No it's not! I'm scratching my head here trying to figure out what that is. I understand the `int a` vs `int& a` thing, but still lost on the reference to pointer. If it's not a trouble, an article link or something I can read to wrap my head around this, perhaps? later will be fine 👍
27th Sep 2019, 4:00 PM
Ipang
+ 1
Let me whip up some code. :)
27th Sep 2019, 4:00 PM
Schindlabua
Schindlabua - avatar
+ 1
When we create some dynamically variable and object then ,at the end of program we must free the variables if we can't do that so it keep memory space and not freed it.. some stage of memory are full and we don't have more memory to run anykind of program in memory .... this phenomenon calls the memory leaking....
28th Sep 2019, 1:32 PM
Chirag Panchal
Chirag Panchal - avatar
0
Memory leak occurs when programmers create a memory in heap and forget to delete it. Memory leaks are particularly serious issues for programs like daemons and servers which by definition never terminate.
29th Sep 2019, 7:19 AM
Dilji
Dilji - avatar
0
g
29th Sep 2019, 10:55 AM
Maha Deva
Maha Deva - avatar