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
Why is it called c++?
Should a constructor be public or private?
Why can templates only be implemented in the header file?
What are the defining traits of an object-oriented language?
How will you call C functions from C ++ and vice-versa?
what is Loop function? What are different types of Loops?
What is a class template?
How would you find out if a linked-list is a cycle or not?
What is a storage class? Mention the storage classes in c++.
What is double in c++?
What is a breakpoint?
What is split a string in c++?
What is pure virtual function?
What data structure is fastest, on average, for retrieving data: a) Binary Tree b) Hash Table c) Stack
How to demonstrate the use of a variable?