K
Keith Thompson
SM Ryan said:Whine to X3 committee of ANSI then. X3.159-1989, section 3.3.4
An arbitrary integer may be converted to a pointer.
The result is implementation defined.
Sounds portable to me.
[ Deliberately obnoxious quoting character corrected *yet again*. ]
Note the distinction between "implementation defined" and "portable".
The code is portable in that it's legal on all implementations, but
it's not guaranteed to do what you want.
Presumably ((void*)-1) is intended to be a unique pointer value that
doesn't necessarily point to any object, basically a secondary null
pointer value. The standard doesn't guarantee that this value is
actually unique. It's possible, though unlikely, that it could happen
to be equal to some valid pointer value. It could even be equal to
NULL.
And that's assuming that the conversion from int to void* behaves
sensibly. That's usually a safe assumption, but the standard doesn't
guarantee it. The only real guarantee about pointer/integer
conversions is that converting a void* to intptr_t or uintptr_t and
back again yields the original value, *if* intptr_t exists; even
changing -1 to (intptr_t)-1 doesn't take advantage of this, since -1
wasn't obtained by converting a void* value.
It's likely to work just fine on most or all real-world
implementations -- or it could break mysteriously when it's ported to
a new platform, forcing you to waste a week or two tracking down the
cause of the misbehavior.
And it's all unnecessary. If you want a unique void* value other than
null, just declare an unused object and take its address:
static int hidden_object;
#define ERROR_INDICATOR (void*)&hidden_object
This has the added benefit of giving a meaningful name to the unique
value, rather than inducing future maintainers to repeat this very
argument.