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


Please Help Members By Posting Answers For Below Questions

What is the use of typedef?

835


What does 7/9*9 equal ? a) 1 b) 0.08642 c) 0

756


What is c++ course?

790


You have two pairs: new() and delete() and another pair : alloc() and free(). Explain differences between eg. New() and malloc()

818


What is the use of c++ programming language in real life?

774


How can we check whether the contents of two structure variables are same or not?

823


Explain the difference between c & c++?

812


How do I run a program in notepad ++?

809


What does asterisk mean in c++?

827


What is meant by forward referencing and when should it be used?

776


What is command line arguments in C++? What are its uses? Where we have to use this?

860


Why is the function main() special?

859


What is stack unwinding?

804


What are the uses of static class data?

872


What is purpose of abstract class?

796