Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is a Dangling pointer? Briefly explane with example.
#1
A dangling pointer is a pointer which has been allocated but does not point to any entity. For example un-initialized pointer.
int * ptr;
*ptr = 10;

The correct way of doing so is:
int * ptr;
int a;
ptr = &a;
*ptr = 10;


Another example can be a function returning reference of a local variable. The local variables are destroyed when function call ends, so if you try to access that location which has been deleted, it may lead you to unpredictable results.

Code:
int * function() { int a = 10; return &a; } int* ptr = function(); *ptr = 5;

0300-9520262
Get solution in 24 hour
|cpmputer & internetConfusedmileys81:[SIZE="4"][SIZE="3"]....[/SIZE][/SIZE]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Dangling Pointers Explain in C++ Vuhelper 0 7,270 05-31-2012, 01:52 AM
Last Post: Vuhelper
  Explain the difference b/w Null Pointer and Null Nodes? C++ 2012 Vuhelper 0 1,951 01-06-2012, 12:24 AM
Last Post: Vuhelper
  call by value, call by reference,call by pointer in data structure 2012 Vuhelper 0 4,333 01-04-2012, 10:28 PM
Last Post: Vuhelper
  Pointer question?? Vuhelper 0 1,420 08-30-2011, 09:55 PM
Last Post: Vuhelper
  C++ have Virtual Function vs Function Pointer call 2011 Xpert 0 5,918 05-12-2011, 09:03 PM
Last Post: Xpert
  what is cursor and why we use it. what is difference between cursor and pointer. saneha 1 7,711 05-02-2011, 06:47 PM
Last Post: Vuhelper
  this pointer in c++ saneha 1 2,411 05-02-2011, 06:41 PM
Last Post: Vuhelper
  Dangling pointer memory leakage Data operations saneha 0 2,501 05-02-2011, 05:43 PM
Last Post: saneha
  In aggregation we use a pointer e.g. like chair object in a room class saneha 1 2,555 05-02-2011, 04:48 PM
Last Post: Vuhelper
  const. pointer or pointer to const. char saneha 1 2,729 04-29-2011, 08:47 AM
Last Post: Vuhelper

Forum Jump:


Users browsing this thread: 1 Guest(s)