🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #402

Re: (char *)(-1)

#402
From: David Keppel
Date: Tue, 01 Aug 1989 19:00
42 lines
1767 bytes
meulenbr@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@cs.washington.edu
    {rutgers,cornell,ucsd,ubc-cs,tektronix}!uw-beaver!june!pardo
Technical Details
Message-ID: <8872@june.cs.washington.edu>
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!cornell!uw-beaver!uw-june!pardo
References: <GEOFF.89Jun11231924@onyx.cs.warwick.ac.uk> <2619@yunexus.UUCP> <388@celit.UUCP> <118@psitech.UUCP> <1063@tukki.jyu.fi> <1282@cbnewsl.ATT.COM> <617@prles2.UUCP>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!cornell!uw-beaver!uw-june!pardo
From: pardo@june.cs.washington.edu (David Keppel)
Newsgroups: comp.std.c
Subject: Re: (char *)(-1)
Keywords: pointers, casts
Message-ID: <8872@june.cs.washington.edu>
Date: 1 Aug 89 19:00:19 GMT
References: <GEOFF.89Jun11231924@onyx.cs.warwick.ac.uk> <2619@yunexus.UUCP> <388@celit.UUCP> <118@psitech.UUCP> <1063@tukki.jyu.fi> <1282@cbnewsl.ATT.COM> <617@prles2.UUCP>
Reply-To: pardo@june.cs.washington.edu (David Keppel)
Organization: University of Washington, Computer Science, Seattle
Lines: 42