Re: assigning pointer to NULL
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Thu, 31 Jan 2008 09:13:07 -0800
"Joachim Schmitz" <nospam.jojo@xxxxxxxxxxxxxxxxxx> writes:
Chris Dollin wrote:
karthikbalaguru wrote:Not quite: you program will crash right there, when dereferencing the NULL
On Jan 31, 5:15 am, "Roman Mashak" <m...@xxxxxxxx> wrote:
is it legal to destroy the memory pointed to by a pointer and then
assign NULL to it? Say, like this:
char *ptr;
ptr = malloc(10);
free(ptr);
Here, ptr will become a Dangling Pointer.
ptr = NULL;
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).
pointer, rather than at some other obscure and random place, so this helps
in detecting the bug.
Probably. There's no guarantee that an attempt to dereference a null
pointer will be detected at run time, (though it will be on most
systems).
--
Keith Thompson (The_Other_Keith) <kst-u@xxxxxxx>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.
- References:
- assigning pointer to NULL
- From: Roman Mashak
- Re: assigning pointer to NULL
- From: karthikbalaguru
- Re: assigning pointer to NULL
- From: Chris Dollin
- Re: assigning pointer to NULL
- From: Joachim Schmitz
- assigning pointer to NULL
- Prev by Date: Re: A solution for the allocation failures problem
- Next by Date: Re: Quick check on signed promotion of bytes
- Previous by thread: Re: assigning pointer to NULL
- Next by thread: Re: assigning pointer to NULL
- Index(es):
Relevant Pages
|