What is dangling pointers?and what is memory leak?

Answers were Sorted based on User's Feedback



What is dangling pointers?and what is memory leak? ..

Answer / 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

What is dangling pointers?and what is memory leak? ..

Answer / prakash

dangling pointer : Dangling pointers in computer
programming are pointers that do not point to a valid
object of the appropriate type. Dangling pointers arise
when an object is deleted or deallocated, without modifying
the value of the pointer, so that the pointer still points
to the memory location of the deallocated memory.

memory leak:A memory leak in computer science is a
particular type of unintentional memory consumption by a
computer program where the program fails to release memory
when no longer needed.
ex:
void f(void)
{
void* s;
s = malloc(50); /* get memory */
return;
}
//control comes out w/o freeing the memory....

Is This Answer Correct ?    70 Yes 13 No

What is dangling pointers?and what is memory leak? ..

Answer / kush

a dangling pointer is a pointer which points to a dead location in memory, means the variable or object is exist on that memory location bt that was deleted and pointer is still pointing to that location . this pointer is called dangling pointer.

EX:
int *function()
{
int a=500;
return &a;
}

int main()
{
int *ptr;
ptr=function();
++*ptr;
printf("%d",*ptr);//output=501
}
in the above example one function is there which is having return type int * means this function retuns a address.
now came int main(), i have creating one pointer *ptr and after that calling the function then whatever the address return by function is stored in *ptr.
but see the storage class of variable a in function it is auto by default and when function execution is end that variable is destroyed and funtion will return the address of variable a.
tha variable is destroy bt my *ptr is still pointing and utilizing that meroy location and data on that location this is called dangling pointer..

thanks...

Is This Answer Correct ?    28 Yes 5 No

What is dangling pointers?and what is memory leak? ..

Answer / shamoona

Dangling Pointer
dangling pointer point to unknown destination
for example
int*ptr;
{
int a=5;
ptr=

Is This Answer Correct ?    1 Yes 4 No

What is dangling pointers?and what is memory leak? ..

Answer / kusuma

points to memory allocation.

Is This Answer Correct ?    8 Yes 31 No

Post New Answer

More C++ General Interview Questions

What are the different data types present in C++?

1 Answers  


How does class accomplish data hiding in c++?

0 Answers  


What does '\r' and '\b' mean? Please explain with example.

7 Answers  


What are friend classes? What are advantages of using friend classes?

0 Answers  


Is c++ harder than java?

0 Answers  






How do you write a function that can reverse a linked-list?

0 Answers  


class Foo { public: Foo(int i) { } }; class Bar : virtual Foo { public: Bar() { } }; Bar b; Referring to the above code, when the object 'b' is defined, a compiler error will occur. What action fixes the compiler error? a) Adding a virtual destructor to the class Bar b) Adding a constructor to Bar which takes an int parameter c) Adding "Foo()" to the Bar constructor d) Adding a copy constructor to the class Foo e) Adding "Foo(0)" to the Bar::Bar initializer list

2 Answers   Quark,


Evaluate the following expression as C++ would do :8 * 9 + 2 * 5 a) 82 b) 79 c) 370 d) list

0 Answers  


Why pointer is used in c++?

0 Answers  


What is the difference between #define debug 0 and #undef debug?

0 Answers  


What is the iunknown interface?

0 Answers  


What is the Maximum Size that an Array can hold?

55 Answers   Adobe, FutureSoft, HCL, Infosys, Satyam, TCS, Wipro,


Categories