Dangling Pointers and Wild pointers

1 view
Skip to first unread message

Venkatesh Raja

unread,
Feb 1, 2005, 5:17:36 AM2/1/05
to cppf...@googlegroups.com
Dangling pointers:
In C and C++, a pointer may be used to hold the address of
dynamically allocated memory. After this memory is freed with the free
function (in C) or delete operator (in C++) the pointer itself will
still contain the address of the released block. This is referred to
as a dangling pointer. Using the pointer in this state is a serious
programming error. Pointers should be assigned 0 in C++, or null in C
after freeing memory to avoid this bug.

Wild Pointer
A pointer that doesn't point to either a valid object (of the
indicated type, if applicable), or to a distinguished null value, if
applicable. (In CeeLanguage/CeePlusPlus, pointers one element past the
end of an array are legal but not dereferenceable; they are very
similar to NullPointers in this regard).

Senthil raj

unread,
Feb 1, 2005, 5:34:35 AM2/1/05
to CppF...@googlegroups.com
Dangling pointers can be better explained using the following example.

int * function()
{
int A[5] = {1, 2, 3, 4, 5};
return(A);
}

int main() {

int *array = function();

for (int i=0;i<5;i++)
printf("%d",array[i[);
}


Even though, the program may appear as perfect, it has a bug. Since
the allocation remains active only during the execution of the
function ( ). After retruning from the function, the array gets
deallocated. So printing the values inside the main () prints garbage
values. These are what u call it as DANGLING pointers.
--
SeNtHiL rAj,
Reply all
Reply to author
Forward
0 new messages