oldwolf@inspire.net.nz (Old Wolf) writes:
[color=blue][color=green]
> > We all know that this:
> >
> > void *p;
> > if (p=malloc(1)) {
> > free(p);
> > p;
> > }
> >
> > causes undefined behaviour if malloc() succeeds.[/color]
>
> Call me Mr. Deer-in-Headlights, but I'd always been taught that
> p;
> is a no-op.[/color]
It's not a no-op from the viewpoint of the standard. Rather, it
calculates the value of variable `p'. Because the value of
variable `p' is indeterminate (see C99 6.2.4#2) and thus may be a
trap representation (see C99 3.17.2), that expression invokes
undefined behavior.
[color=blue]
> Furthermore, how about
:
>
> if (p=malloc(1)) {
> int x = (int)p;
> free(p);
> x;
> }[/color]
No pointer is involved, so if the conversion `(int) p' produced
a value that is not a trap representation, it is still not a trap
representation after the free().
[color=blue]
> Casting a pointer to an int is implementation-defined (but not
> undefined), isn't it?[/color]
Not necessarily, see C99 6.3.2.3#6:
If the result cannot be represented in the integer type, the
behavior is undefined.
[color=blue]
> There does not seem to be much difference between the above, and
> if (p=malloc(1)) {
> free(p);
> int x = (int)p;
> x;
> }[/color]
That's undefined too, because the value of `p' is still being
accessed. You could access the bytes in `p' through a character
pointer if you like, though.
[color=blue]
> My rationale: the reason it's undefined to use a pointer value (without
> dereferencing) is for systems where loading such a value to a memory
> access register causes an exception.[/color]
That's a reasonable reason.
[color=blue]
> However, if we are just converting to int, that register would
> not have to be used.[/color]
But it's not required not to be used. Behavior is still
undefined.
[color=blue]
> Since the int cast is implementation-defined, the
> implementation could always do this without doing anything
> undefined.[/color]
--
"Welcome to the wonderful world of undefined behavior, where the demons
are nasal and the DeathStation users are nervous." --Daniel Fox