+ 12
What is memory leak? can anyone explain with example
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.
+ 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.
+ 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.
+ 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.
+ 5
Yeah but C++ also introduced smart pointers which do all the deleting/freeing stuff for us automatically.
+ 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?
+ 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) ✌
+ 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 👍
+ 1
Let me whip up some code. :)
+ 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....
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.
0
g