Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > assigning pointer to NULL

Reply
Thread Tools

assigning pointer to NULL

 
 
Ian Collins
Guest
Posts: n/a
 
      01-30-2008
Roman Mashak wrote:
> Hello,
>
> 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);
> ptr = NULL;
>

Yes, you can assign what ever you like to a freed pointer. Assignment
to NULL is often used to mark the memory as freed.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      01-30-2008
Roman Mashak wrote:

> Hello,
>
> 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);
> ptr = NULL;


Certainly. This is common practise.

 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      01-30-2008
"Roman Mashak" <(E-Mail Removed)> 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);
> ptr = NULL;


Legal, but useless. If you think you've found the royal road to memory
management, consider the following:

char *ptr, &ptr2;
ptr=malloc(10);
ptr2=ptr+2;
free(ptr);
ptr=NULL;
if (ptr+2) crash_hard_and_without_grace();

Richard
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      01-30-2008

"Roman Mashak" <(E-Mail Removed)> wrote in message
> 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);
> ptr = NULL;
>

Yes. As Richard Bos says. it won't solve all your memory management
problems. But if the pointer will not immediately go out of scope, it is
good idea to make it null to mark that it is now invalid.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

 
Reply With Quote
 
vippstar@gmail.com
Guest
Posts: n/a
 
      01-30-2008
On Jan 31, 2:15 am, "Roman Mashak" <(E-Mail Removed)> wrote:
> Hello,
>
> 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);
> ptr = NULL;
>
> With best regards, Roman Mashak. E-mail: <removed>


This is done because then you can do free(ptr); which will be
free(NULL); and that does nothing.
 
Reply With Quote
 
Randy Howard
Guest
Posts: n/a
 
      01-30-2008
On Wed, 30 Jan 2008 07:00:38 -0600, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote
(in article
<(E-Mail Removed)>):

> On Jan 31, 2:15 am, "Roman Mashak" <(E-Mail Removed)> wrote:
>> Hello,
>>
>> 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);
>> ptr = NULL;
>>
>> With best regards, Roman Mashak. E-mail: <removed>

>
> This is done because then you can do free(ptr); which will be
> free(NULL); and that does nothing.


Historically, that was not always the case.

--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw





 
Reply With Quote
 
vippstar@gmail.com
Guest
Posts: n/a
 
      01-30-2008
On Jan 30, 3:45 pm, Randy Howard <(E-Mail Removed)>
wrote:
> On Wed, 30 Jan 2008 07:00:38 -0600, (E-Mail Removed) wrote
> (in article
> <(E-Mail Removed)>):
>
> > On Jan 31, 2:15 am, "Roman Mashak" <(E-Mail Removed)> wrote:
> >> Hello,

>
> >> 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);
> >> ptr = NULL;

>
> >> With best regards, Roman Mashak. E-mail: <removed>

>
> > This is done because then you can do free(ptr); which will be
> > free(NULL); and that does nothing.

>
> Historically, that was not always the case.

True, back then I suppose they assigned a freed pointer to NULL so
when they use a freed pointer they get a NULL address access
violation.
 
Reply With Quote
 
dj3vande@csclub.uwaterloo.ca.invalid
Guest
Posts: n/a
 
      01-30-2008
In article <(E-Mail Removed)4all.nl>,
Richard Bos <(E-Mail Removed)> wrote:
>"Roman Mashak" <(E-Mail Removed)> 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);
>> ptr = NULL;

>
>Legal, but useless.


Not completely useless, only mostly.

If you know that the pointer you're setting to NULL was the only
pointer into that memory, then it's perfectly safe and useful.
(This is rare, but not unheard of.)

It's also not unreasonable to use the non-null-ness of a pointer as a
flag that you have useful information of some sort, and to want to
discard that information before you have something else to replace it
with.
(There are intelligent people who disagree with other intelligent
people about whether this indicates a design flaw.)


dave

--
Dave Vandervies dj3vande at eskimo dot com
I like "fun" risks, rather than "lazy" risks.
(If I'm going to kill myself, I want to have a good time on the way.)
--Graham Reed in the scary devil monastery
 
Reply With Quote
 
ymuntyan@gmail.com
Guest
Posts: n/a
 
      01-30-2008
On Jan 30, 11:25 am, (E-Mail Removed) wrote:
> In article <(E-Mail Removed)4all.nl>,
>
> Richard Bos <(E-Mail Removed)> wrote:
> >"Roman Mashak" <(E-Mail Removed)> 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);
> >> ptr = NULL;

>
> >Legal, but useless.

>
> Not completely useless, only mostly.
>
> If you know that the pointer you're setting to NULL was the only
> pointer into that memory, then it's perfectly safe and useful.
> (This is rare, but not unheard of.)
>
> It's also not unreasonable to use the non-null-ness of a pointer as a
> flag that you have useful information of some sort, and to want to
> discard that information before you have something else to replace it
> with.
> (There are intelligent people who disagree with other intelligent
> people about whether this indicates a design flaw.)


It's UB if you access the value of the pointer after you
call free() on it. So you simply can't (in comp.lang.c)
do stuff like

free(ptr);
....
if (ptr)
it_wasnt_null();

Yevgen
 
Reply With Quote
 
karthikbalaguru
Guest
Posts: n/a
 
      01-30-2008
On Jan 31, 5:15*am, "Roman Mashak" <(E-Mail Removed)> wrote:
> Hello,
>
> 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 .

Karthik Balaguru
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to pointer intialize to NULL but still point to NULL Christopher C++ 4 07-09-2011 12:35 AM
Null pointer (NULL array pointer is passed) aneuryzma C++ 3 06-16-2008 05:48 AM
Assigning NULL to a variable (not pointer) leonm54@gmail.com C++ 31 10-17-2006 05:33 PM
Assigning methods to objects, and assigning onreadystatechange to an XMLHttpRequest -- an inconsistency? weston Javascript 1 09-22-2006 09:33 AM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM



Advertisments