assigning pointer to NULL

Discussion in 'C Programming' started by Ian Collins, Jan 30, 2008.

  1. Ian Collins

    Ian Collins Guest

    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.
    Ian Collins, Jan 30, 2008
    #1
    1. Advertising

  2. Ian Collins

    santosh Guest

    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.
    santosh, Jan 30, 2008
    #2
    1. Advertising

  3. Ian Collins

    Richard Bos Guest

    "Roman Mashak" <> 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
    Richard Bos, Jan 30, 2008
    #3
  4. "Roman Mashak" <> 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
    Malcolm McLean, Jan 30, 2008
    #4
  5. Ian Collins

    Guest

    On Jan 31, 2:15 am, "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;
    >
    > 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.
    , Jan 30, 2008
    #5
  6. Ian Collins

    Randy Howard Guest

    On Wed, 30 Jan 2008 07:00:38 -0600, wrote
    (in article
    <>):

    > On Jan 31, 2:15 am, "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;
    >>
    >> 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
    Randy Howard, Jan 30, 2008
    #6
  7. Ian Collins

    Guest

    On Jan 30, 3:45 pm, Randy Howard <>
    wrote:
    > On Wed, 30 Jan 2008 07:00:38 -0600, wrote
    > (in article
    > <>):
    >
    > > On Jan 31, 2:15 am, "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;

    >
    > >> 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.
    , Jan 30, 2008
    #7
  8. Ian Collins

    Guest

    In article <4all.nl>,
    Richard Bos <> wrote:
    >"Roman Mashak" <> 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
    , Jan 30, 2008
    #8
  9. Ian Collins

    Guest

    On Jan 30, 11:25 am, wrote:
    > In article <4all.nl>,
    >
    > Richard Bos <> wrote:
    > >"Roman Mashak" <> 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
    , Jan 30, 2008
    #9
  10. On Jan 31, 5:15 am, "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);


    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
    karthikbalaguru, Jan 30, 2008
    #10
  11. Ian Collins

    Chris Dollin Guest

    karthikbalaguru wrote:

    > On Jan 31, 5:15 am, "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);

    >
    > 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.
    Chris Dollin, Jan 30, 2008
    #11
  12. Ian Collins

    Flash Gordon Guest

    wrote, On 30/01/08 18:11:
    > On Jan 30, 11:25 am, wrote:
    >> In article <4all.nl>,
    >>
    >> Richard Bos <> wrote:
    >>> "Roman Mashak" <> 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;
    Flash Gordon, Jan 30, 2008
    #12
  13. "Chris Dollin" <> 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
    Malcolm McLean, Jan 30, 2008
    #13
  14. Ian Collins

    Army1987 Guest

    karthikbalaguru wrote:
    > On Jan 31, 5:15 am, "Roman Mashak" <> 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")
    Army1987, Jan 30, 2008
    #14
  15. Ian Collins

    user923005 Guest

    On Jan 30, 1:01 am, (Richard Bos) wrote:
    > "Roman Mashak" <> 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 helps 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.
    user923005, Jan 30, 2008
    #15
  16. Ian Collins

    Roman Mashak Guest

    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:
    Roman Mashak, Jan 31, 2008
    #16
  17. Chris Dollin wrote:
    > karthikbalaguru wrote:
    >
    >> On Jan 31, 5:15 am, "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);

    >>
    >> 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 helps
    in detecting the bug.

    Bye, Jojo
    Joachim Schmitz, Jan 31, 2008
    #17
  18. Army1987 wrote:
    > karthikbalaguru wrote:
    >> On Jan 31, 5:15 am, "Roman Mashak" <> 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
    Joachim Schmitz, Jan 31, 2008
    #18
  19. user923005 wrote:
    > On Jan 30, 1:01 am, (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 helps 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
    Joachim Schmitz, Jan 31, 2008
    #19
  20. Ian Collins

    Ian Collins Guest

    Joachim Schmitz wrote:
    > user923005 wrote:
    >> On Jan 30, 1:01 am, (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 helps 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.
    Ian Collins, Jan 31, 2008
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    26,482
    Mike Schilling
    Mar 29, 2006
  2. Replies:
    31
    Views:
    830
    Ron Natalie
    Oct 17, 2006
  3. aneuryzma
    Replies:
    3
    Views:
    690
    Jim Langston
    Jun 16, 2008
  4. Christopher
    Replies:
    4
    Views:
    425
    Ruben Safir
    Jul 9, 2011
  5. weston
    Replies:
    1
    Views:
    238
    Richard Cornford
    Sep 22, 2006
Loading...

Share This Page