PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > assigning pointer to NULL
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
assigning pointer to NULL

Réponse
 
LinkBack Outils de la discussion
Vieux 30/01/2008, 07h38   #1
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assigning pointer to NULL

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.
  Réponse avec citation
Vieux 30/01/2008, 08h57   #2
santosh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assigning pointer to NULL

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.

  Réponse avec citation
Vieux 30/01/2008, 09h01   #3
Richard Bos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assigning pointer to NULL

"Roman Mashak" <mrv@tusur.ru> 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
  Réponse avec citation
Vieux 30/01/2008, 10h13   #4
Malcolm McLean
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assigning pointer to NULL


"Roman Mashak" <mrv@tusur.ru> 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

  Réponse avec citation
Vieux 30/01/2008, 13h00   #5
vippstar@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assigning pointer to NULL

On Jan 31, 2:15 am, "Roman Mashak" <m...@tusur.ru> 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.
  Réponse avec citation
Vieux 30/01/2008, 13h45   #6
Randy Howard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assigning pointer to NULL

On Wed, 30 Jan 2008 07:00:38 -0600, vippstar@gmail.com wrote
(in article
<31d018f4-f8eb-4121-801c-329281a0cba7@q21g2000hsa.googlegroups.com>):

> On Jan 31, 2:15 am, "Roman Mashak" <m...@tusur.ru> 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





  Réponse avec citation
Vieux 30/01/2008, 13h53   #7
vippstar@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assigning pointer to NULL

On Jan 30, 3:45 pm, Randy Howard <randyhow...@FOOverizonBAR.net>
wrote:
> On Wed, 30 Jan 2008 07:00:38 -0600, vipps...@gmail.com wrote
> (in article
> <31d018f4-f8eb-4121-801c-329281a0c...@q21g2000hsa.googlegroups.com>):
>
> > On Jan 31, 2:15 am, "Roman Mashak" <m...@tusur.ru> 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.
  Réponse avec citation
Vieux 30/01/2008, 17h25   #8
dj3vande@csclub.uwaterloo.ca.invalid
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assigning pointer to NULL

In article <47a03c63.2858170722@news.xs4all.nl>,
Richard Bos <rlb@hoekstra-uitgeverij.nl> wrote:
>"Roman Mashak" <mrv@tusur.ru> 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
  Réponse avec citation
Vieux 30/01/2008, 18h11   #9
ymuntyan@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assigning pointer to NULL

On Jan 30, 11:25 am, dj3va...@csclub.uwaterloo.ca.invalid wrote:
> In article <47a03c63.2858170...@news.xs4all.nl>,
>
> Richard Bos <r...@hoekstra-uitgeverij.nl> wrote:
> >"Roman Mashak" <m...@tusur.ru> 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
  Réponse avec citation
Vieux 30/01/2008, 18h19   #10
karthikbalaguru
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assigning pointer to NULL

On Jan 31, 5:15am, "Roman Mashak" <m...@tusur.ru> 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
  Réponse avec citation
Vieux 30/01/2008, 18h38   #11
Chris Dollin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assigning pointer to NULL

karthikbalaguru wrote:

> On Jan 31, 5:15Âam, "Roman Mashak" <m...@tusur.ru> 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 .


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.

--
Commoner Hedgehog
Nit-picking is best done among friends.

  Réponse avec citation
Vieux 30/01/2008, 19h42   #12
Flash Gordon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assigning pointer to NULL

ymuntyan@gmail.com wrote, On 30/01/08 18:11:
> On Jan 30, 11:25 am, dj3va...@csclub.uwaterloo.ca.invalid wrote:
>> In article <47a03c63.2858170...@news.xs4all.nl>,
>>
>> Richard Bos <r...@hoekstra-uitgeverij.nl> wrote:
>>> "Roman Mashak" <m...@tusur.ru> 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();


However it is legal to do:

if (something) {
free(ptr);
ptr = NULL;
}
...
if (ptr)
if_was_still_valid();
--
Flash Gordon;
  Réponse avec citation
Vieux 30/01/2008, 20h25   #13
Malcolm McLean
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assigning pointer to NULL


"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

  Réponse avec citation
Vieux 30/01/2008, 20h55   #14
Army1987
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assigning pointer to NULL

karthikbalaguru wrote:
> On Jan 31, 5:15Âam, "Roman Mashak" <m...@tusur.ru> wrote:
>> free(ptr);
>> 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 .

Not necessarily, for example, in many cases the pointer to freed memory is
going to immediately go out of scope.


--
Army1987 (Replace "NOSPAM" with "email")
  Réponse avec citation
Vieux 30/01/2008, 21h11   #15
user923005
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assigning pointer to NULL

On Jan 30, 1:01am, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
> "Roman Mashak" <m...@tusur.ru> 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();


I don't think there are any magical cures for undefined behavior
caused by programming errors. The program may also crash hard and
without grace at the free(ptr) function call (or worse, of course).
In general, I think that setting a freed pointer to null is a good
idea. The fact that it won't cure every sort of pointer illness
doesn't negate the small value received in recompense.

IMO-YMMV.

P.S.
I almost always set freed pointers to null myself (the exception being
in C++ because it is pointless to set a deleted class member pointer
to null in a destructor.)
It s with a very small class of problems (but does have one bad
side effect -- double frees are less likely to be detected) but
overall I think it is a tiny win.
  Réponse avec citation
Vieux 31/01/2008, 00h15   #16
Roman Mashak
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut assigning pointer to NULL

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: mrv@tusur.ru


  Réponse avec citation
Vieux 31/01/2008, 07h40   #17
Joachim Schmitz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assigning pointer to NULL

Chris Dollin wrote:
> karthikbalaguru wrote:
>
>> On Jan 31, 5:15 am, "Roman Mashak" <m...@tusur.ru> 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 .

>
> 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).

Not quite: you program will crash right there, when dereferencing the NULL
pointer, rather than at some other obscure and random place, so this s
in detecting the bug.

Bye, Jojo


  Réponse avec citation
Vieux 31/01/2008, 07h43   #18
Joachim Schmitz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assigning pointer to NULL

Army1987 wrote:
> karthikbalaguru wrote:
>> On Jan 31, 5:15 am, "Roman Mashak" <m...@tusur.ru> wrote:
>>> free(ptr);
>>> 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 .

> Not necessarily, for example, in many cases the pointer to freed
> memory is going to immediately go out of scope.

In which case there is some work to be done by the optimizer: detect this
and drop the assignment

Bye, Jojo


  Réponse avec citation
Vieux 31/01/2008, 07h51   #19
Joachim Schmitz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assigning pointer to NULL

user923005 wrote:
> On Jan 30, 1:01 am, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:

<snip>
> P.S.
> I almost always set freed pointers to null myself (the exception being
> in C++ because it is pointless to set a deleted class member pointer
> to null in a destructor.)
> It s with a very small class of problems (but does have one bad
> side effect -- double frees are less likely to be detected)

But they won't harm (i.e. possibly crash the program) anymore either.

Bye, Jojo


  Réponse avec citation
Vieux 31/01/2008, 08h00   #20
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assigning pointer to NULL

Joachim Schmitz wrote:
> user923005 wrote:
>> On Jan 30, 1:01 am, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:

> <snip>
>> P.S.
>> I almost always set freed pointers to null myself (the exception being
>> in C++ because it is pointless to set a deleted class member pointer
>> to null in a destructor.)
>> It s with a very small class of problems (but does have one bad
>> side effect -- double frees are less likely to be detected)

> But they won't harm (i.e. possibly crash the program) anymore either.
>

But the effect can be detrimental on a system (which includes most
desktop environments) where the allocator can detect duplicate frees.

For that reason, I never set a freed pointer to NULL.

--
Ian Collins.
  Réponse avec citation
Vieux 31/01/2008, 08h20   #21
santosh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assigning pointer to NULL

Ian Collins wrote:

> Joachim Schmitz wrote:
>> user923005 wrote:
>>> On Jan 30, 1:01 am, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:

>> <snip>
>>> P.S.
>>> I almost always set freed pointers to null myself (the exception
>>> being in C++ because it is pointless to set a deleted class member
>>> pointer to null in a destructor.)
>>> It s with a very small class of problems (but does have one bad
>>> side effect -- double frees are less likely to be detected)

>> But they won't harm (i.e. possibly crash the program) anymore either.
>>

> But the effect can be detrimental on a system (which includes most
> desktop environments) where the allocator can detect duplicate frees.
>
> For that reason, I never set a freed pointer to NULL.


Same here on the basis that, in C at least, the programmer *must* always
be aware of which pointers have valid or invalid values at all points
in his program. Anything short of this is going to lead to bugs. From
this P.O.V. setting dangling pointers to NULL is redundant.

  Réponse avec citation
Vieux 31/01/2008, 17h13   #22
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assigning pointer to NULL

"Joachim Schmitz" <nospam.jojo@schmitz-digital.de> writes:
> Chris Dollin wrote:
>> karthikbalaguru wrote:
>>> On Jan 31, 5:15 am, "Roman Mashak" <m...@tusur.ru> 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).

> Not quite: you program will crash right there, when dereferencing the NULL
> pointer, rather than at some other obscure and random place, so this s
> 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@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  Réponse avec citation
Vieux 31/01/2008, 18h47   #23
Flash Gordon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assigning pointer to NULL

santosh wrote, On 31/01/08 08:20:
> Ian Collins wrote:
>
>> Joachim Schmitz wrote:
>>> user923005 wrote:
>>>> On Jan 30, 1:01 am, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
>>> <snip>
>>>> P.S.
>>>> I almost always set freed pointers to null myself (the exception
>>>> being in C++ because it is pointless to set a deleted class member
>>>> pointer to null in a destructor.)
>>>> It s with a very small class of problems (but does have one bad
>>>> side effect -- double frees are less likely to be detected)
>>> But they won't harm (i.e. possibly crash the program) anymore either.
>>>

>> But the effect can be detrimental on a system (which includes most
>> desktop environments) where the allocator can detect duplicate frees.
>>
>> For that reason, I never set a freed pointer to NULL.

>
> Same here on the basis that, in C at least, the programmer *must* always
> be aware of which pointers have valid or invalid values at all points
> in his program. Anything short of this is going to lead to bugs. From
> this P.O.V. setting dangling pointers to NULL is redundant.


In some situations setting pointers to NULL can be useful and used to
indicate whether it is valid or not. For example, I have some code of
this general form...

char *ptr = NULL;

/* do stuff */

if (cond1) {
ptr = malloc(something);
/* do stuff with ptr */
if (cond2) {
free(ptr);
ptr = NULL;
}
}
/* do stuff not using ptr */
if (cond3) {
/* do stuff using ptr */
free(ptr);
}


Note that the code deliberately has only one pointer to the malloc'd block.
--
Flash Gordon
  Réponse avec citation
Vieux 01/02/2008, 10h05   #24
Richard Bos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assigning pointer to NULL

dj3vande@csclub.uwaterloo.ca.invalid wrote:

> In article <47a03c63.2858170722@news.xs4all.nl>,
> Richard Bos <rlb@hoekstra-uitgeverij.nl> wrote:
> >"Roman Mashak" <mrv@tusur.ru> 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.


It's only safe if you know that it's the only copy, but if you know
that, I don't see the use. You can just not use that one copy you know
about any more.

> 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.)


Once, when memory was as rare as the purifier aether and processors had
two registers a piece, it may have been useful. Now, with optimising
compilers and rarely no registers to spare, just use an extra int.

Richard
  Réponse avec citation
Vieux 02/02/2008, 09h50   #25
Chris Dollin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assigning pointer to NULL

Joachim Schmitz wrote:

> Chris Dollin wrote:
>> 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).

> Not quite: you program will crash right there, when dereferencing the NULL
> pointer,


Perhaps. It's not a requirement and I've used systems where it
didn't. It wouldn't surprise me if (some) embedded systems didn't
read-protect whatever-address-corresponds-to-the-value-of-the-null-pointer,
given that they have to shave margins everywhere. Could an embeddey
person comment?

--
Far-Fetched Hedgehog
A rock is not a fact. A rock is a rock.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h58.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,44874 seconds with 33 queries