Re: ...malloc ...from Rome :-)

From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 10/18/04


Date: Mon, 18 Oct 2004 18:40:31 GMT


"James Stevenson" <james@stev.org> wrote in message
news:Pine.LNX.4.44.0410181504470.6614-100000@beast.stev.org...

> > > free(pInt);
> > > }
> > >
> > > printf("Release memory previously allocated [%p]\n",pInt);
> >
> > This provokes undefined behavior -- you may not reference the value of
> > pInt in any way after it has been passed to free().
>
> Actually its not.
>
> if you define int *p;
> it is perforectly ok to access p but not ok to access *p
> after the free is performed.

I used to believe that too -- until I came to clc and
the kind folks here straightened me out. :-)

> Also what you seem to impliy is that the value of p
> will change once passed to the function free. This is also
> not true.

No, in itself that's not necessarily true. What *is* true
is that the standard specifically states that the result
of evaluating its value is undefined behavior.

>Only the memory address that p points to may / may not be
> changed. The actually value of p will remain constant.

It might, it might not. But that doesn't matter.

-Mike



Relevant Pages

  • Re: Question on protein intake
    ... >> Milk: 1 pint ... "Be who you are and say what you feel, because those who mind don't ... matter, and those who matter don't mind." ...
    (uk.rec.bodybuilding)
  • Re: ...malloc ...from Rome :-)
    ... James Stevenson writes: ... >> pInt in any way after it has been passed to free. ... accessing the value of p after freeinvokes undefined behavior. ... On most real-world systems, the resulting ...
    (comp.lang.c)
  • Re: Kids eh? Dad!, my screens gone all funny!
    ... Why the hell do they call it a mouse ... I'm off to drink a pint of vodka at my local. ... you like and it doesn't matter at all. ...
    (uk.comp.sys.mac)
  • Re: Kids eh? Dad!, my screens gone all funny!
    ... I'm off to drink a pint of vodka at my local. ... Vodka, magma, beer, pus... ... you like and it doesn't matter at all. ...
    (uk.comp.sys.mac)
  • Re: Kids eh? Dad!, my screens gone all funny!
    ... I'm off to drink a pint of vodka at my local. ... Vodka, magma, beer, pus... ... you like and it doesn't matter at all. ...
    (uk.comp.sys.mac)