On Mon, 22 Dec 2003 01:38:32 UTC, "E. Robert Tisdale"
<> wrote:
> Andrew Clark wrote:
>
> > Someone brought this up in a group I read on Yahoo!
> >
> > char* p = (char*)malloc(16);
> > if (NULL != p) {
> > char* q = p;
> > p = NULL;
> > free(q);
> > }
> >
> > Will this invoke undefined behavior?
>
> No. Neither will this
Wrong. It occures undefined behavior when you forget to include
stdlib.h. You hides the diagnostic saying that you tries to convert
int to a pointer throu casting the value the compiler things it were
int to a pointer.
> char* p = (char*)malloc(16);
The same bug as above - resulting in undefined behavior.
> char* q = p;
> p = NULL;
> free(q);
>
That's fine - but lets q hold an indetermined value (the pointer in it
points to somewhere but not to an address guilty for your program)
after free.
--
Tschau/Bye
Herbert
Visit
http://www.ecomstation.de the home of german eComStation