edited by
5,170 views
0 votes
0 votes

A memory leak happens when
a) a program allocates memory in heap but forgets to deallocate it
b) when an un-assigned pointer is used is freed using free function
c) when realloc() is called on a pointer that is not allocated
d) A program allocates memory in stack

edited by

3 Answers

0 votes
0 votes

Option A) will be correct

Memory leak causes when we allocate a memory using dynamic memory allocation (like malloc, calloc, realloc) but forget to deallocate it. After all working is done, we have to deallocate the memory calling free function. But if we forget to call free function, In heap memory still allocated with previous value and no new work can be done in that portion of memory. That is generally called memory leak.

For more information

https://gateoverflow.in/165178/c-coding

0 votes
0 votes

Memory leak happens when you create a memory in heap but you forget to deallocate it

For example

You have a pointer x pointing to variable a

now you make the pointer x point to variable b

variable a is still in memory and can never be accessed

this leads to memory leak

Therefore Option A with a slight variation in it ,.......

It happenes when u forget to deallocate..... not allocate

change that in your option :)

0 votes
0 votes

Memory leak made simple: whenever you allocate memory with malloc/new and don't deallocate it later with free/delete after finishing using that memory... will cause a memory leak! The allocated memory will remain there and that space won't be used by your program ever again.

This is a serious problem when the leak is on a function that is called many times, rendering the leak to grow larger and larger each time the function is called.

As you continue to do this you will eventually run out of space on the heap that you can allocate to, and you're program will get close and closer to blowing up because it has no memory to work with among other problems.

 

To conclude: It is a memory leak if the memory cannot be reclaimed by the system once the program has finished using a variable.

Related questions

0 votes
0 votes
2 answers
1
Akshay Jindal asked Sep 26, 2015
648 views
Will the output be as per previous values or new values?