(char *)(-1)

David Keppel pardo at june.cs.washington.edu
Wed Aug 2 05:00:19 AEST 1989


meulenbr at cstw01.prl.philips.nl (Frans Meulenbroeks) writes:
>[I'll use (char *)-1, because the chances are so small...]

There is a machine (forget what, read it in comp.arch) that uses a 2's
complement memory model.  What this means is that your memory looks
like:

	|   ^	|
	|   |	|
	|dynamic|
	| data	|
	+-------+
	|	|
	| code	|
	|	|  0
	+-------+
	|	| -1
	| stack	|
	|   |	|
	|   v	|

So here is an instance of (char *)-1 very nearly *guaranteed* to be a
valid char*.

Second, your programs will not be strictly conforming.

My suggested `fix' is to declare a special character.

    char const magic_1 = 'j';
    static char const terminator[] = "unk";
    #define MAGIC_1 (&magic_1)

Then  `return (MAGIC_1);'.  *If* the implementation gives you
contiguous layout, then dereferencing `MAGIC_1' will give you `junk'.
If the implementation doesn't give you contiguous layout, then the
program is still correct (and conformant), but might be a little
harder to debug.

	;-D on  ( No such thing as a `clever' hack? )  Pardo
-- 
		    pardo at cs.washington.edu
    {rutgers,cornell,ucsd,ubc-cs,tektronix}!uw-beaver!june!pardo



More information about the Comp.std.c mailing list