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
Which operator cannot be overloaded c++?
Why is standard template library used?
Can a program run without main?
Why do we use classes in programming?
What are different types of loops in c++?
What is using namespace std in cpp?
What is a virtual destructor? Explain the use of it?
What does namespace mean in c++?
What is the meaning of c++?
What is the difference between while and do while loop? Explain with examples.
Which bitwise operator is used to check whether a particular bit is on or off?
When is the copy constructor called?
What does the following code do: int c=0; cout< a) Undefined *Updated* b) 01 c) 00
Given the following seqment of code containing a group of nested if instructions: y = 9; if ((x==3) || (x == 5)) y++; else if (x == 2) y *= 2; else if (x == ) y-= 7; else y = 8; if the value of x is 4 before the nested IFs are executed, what is the value of y after the nested IFs are executed?
Explain the difference between static and dynamic binding of functions?