What is dangling pointers?and what is memory leak?
Answer Posted / rishabh agrawal
A1 - Dangling pointer points to a memory location which is
being removed.
E.g.
int* a = new int;
int *b = a;
delete b;
Now a will be a dandling pointer.
A2 - Memory leaks occur when memory allocated to a pointer
is not deleted and the pointer goes out of scope. This
allocated memory never gets de-allocated and remains in the
heap until the system is restarted.
E.g.
void foo()
{
int* ptr = new int;
*ptr = 10;
...
...
}
Since ptr is allocated memory using 'new', but no 'delete'
is called, hence memory allocated to ptr will leak.
| Is This Answer Correct ? | 106 Yes | 22 No |
Post New Answer View All Answers
Explain the difference between new() and malloc() in c++?
Write a c program for binary addition of two 8 bit numbers.
Explain the concept of copy constructor?
What happens if a pointer is deleted twice?
Which is most difficult programming language?
What does ctime() do?
What is a NULL Macro? What is the difference between a NULL Pointer and a NULL Macro?
Is python written in c or c++?
What methods can be overridden in java?
What do you mean by late binding?
What is pure virtual function? Or what is abstract class?
What is malloc in c++?
What are the manipulators in c++?
I want explanation for this assignment: how to connect mysql database using c/c++,please explain this detailly?
Describe private, protected and public – the differences and give examples.