(char *)(-1)

Michael Zehr tada at athena.mit.edu
Tue Aug 1 01:21:11 AEST 1989


Someone, somewhere, somewhen, asked:
>Is it safe to return -1?

In article <120 at psitech.UUCP> david at psitech.UUCP (david Fridley) writes:
>To quote from the my UNIX System V (Release 3.0) Programmers Reference manual
>for BRK(2) (page 141):
>[brk,sbrk definitions]
>
>Here we see that sbrk(), a standard UNIX function, which returns a character
>pointer will return (-1).  

Giving an example of software that does not conform strictly to the
dpANS standard for C is not an argument for writing more software like
it.  Not only is returning (char *) -1 (or (void *) -1) not safe for
portability reasons, it's also not a very good programming practice.

The practice of returning zero to indicate failure of some sort is very
firmly entrenched in software development, so go ahead and use that
method -- but don't make it any worse.  If you must return errors like
this, follow the current practice -- 0 (or (void *) 0) means error, with
more detail in a global error code (errno, for example).

It's taken long enough to get a dpANSI standard -- let's at least use it
now!

-michael j zehr



More information about the Comp.std.c mailing list