"Chris Dollin" <eh@electrichedgehog.net> wrote in message
> karthikbalaguru wrote:
>
>> Here, ptr will become a Dangling Pointer.
>>
>> By this assignment of NULL, ptr will no longer be a dangling pointer.
>>
>> This is a very important step to be followed while freeing .
>
> This is /sometimes/ an important step. At other times, it's
> mindblowingly pointless. It depends, for example, on whether
> there are other ways to get to the pointer variable (if not,
> assigning null to it is pointless) and whether those other
> ways bother to check for null (in which case you have problems
> regardless).
>
> In short, there is no royal road to memory management in C.
>
No. A good rule is one pointer to each object, but this goes against the
principle of structured programming that variables should be local. Also,
some data structures, like doubly linked lists, depend on more than one
pointer to each node.
However if you allocate and destroy whole strucutres in matching creat /
free functions, your dangling pointer problems should be minimal.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm