[net.lang.c] Casting call

geacc022%timevx@cit-hamlet.arpa (05/17/85)

>This discussion has brought up another point -- somebody posted an example
>where he casted a lhs, and somebody pointed out that you can't do that.
>What I am wondering is, why can't you? There seem to be no semantic problems
>involved, and it can be very useful for something like:
>
>#define FREE(ptr)	 free(char *) ptr); (char *) (ptr) = (char *) -1;

There are indeed problems with this.  What if you're on a machine
where char * is 32 bits, but int * is only 16 bits?

What you really want is a "typeof" operator:

#define FREE(ptr)     {  free((char *) ptr); ptr = (typeof ptr) NULL;  }

Unfortunately, this doesn't exist either.  :-(

			Gary Ansok
			GEACC022%TIMEVX @ CIT-HAMLET.ARPA
			GEA @ CALTECH.BITNET
			...ucbvax!cithep!timevx#geacc022

"All the world loves a straight man."

guy@sun.uucp (Guy Harris) (05/22/85)

> What you really want is a "typeof" operator:
> 
> #define FREE(ptr)     {  free((char *) ptr); ptr = (typeof ptr) NULL;  }

You don't need it.  If we were using ANSI C, we wouldn't need to cast
null pointers at all, except in calls to "execl" and the like, as long as
we declared the types of arguments to routines; the compiler would know that
a given routine expected a "char *" and would coerce a 0 passed to the
routine into a "(char *)0".  In this case, the compiler does know that
you're assigning 0 to a pointer of a particular type, and that it should
cast the 0 into a "(typeof ptr)0".

	Guy Harris

gwyn@Brl.ARPA (VLD/VMB) (05/23/85)

>> #define FREE(ptr)	{  free((char *) ptr); ptr = (typeof ptr) NULL;  }

> ... If we were using ANSI C, ...

One doesn't even need any of the new ANSI C features:

#define	FREE( ptr )	( free( (char *)ptr ), ptr = NULL )

since one can always assign 0 (NULL) to any pointer type.

faustus@ucbcad.UUCP (Wayne A. Christopher) (05/24/85)

> >> #define FREE(ptr)	{  free((char *) ptr); ptr = (typeof ptr) NULL;  }
> 
> > ... If we were using ANSI C, ...
> 
> One doesn't even need any of the new ANSI C features:
> 
> #define	FREE( ptr )	( free( (char *)ptr ), ptr = NULL )
> 
> since one can always assign 0 (NULL) to any pointer type.

The original problem was that NULL is a valid address under 4.{2,3},
but (char *) -1 isn't. This is, of course, dependent on 4.2 and
the VAX.

	Wayne

guy@sun.uucp (Guy Harris) (05/25/85)

> The original problem was that NULL is a valid address under 4.{2,3},
> but (char *) -1 isn't. This is, of course, dependent on 4.2 and
> the VAX.

The accident of implementation that makes the result of casting NULL into a
pointer be a value that does point to a mapped portion of your address space
doesn't justify using (char *) -1 as a special sentinel value for pointers,
instead of NULL.  On PDP-11 UNIX, an accident of implementation makes the
result of casting -1 into a pointer point to a mapped *and* writable portion
of your address space.  On VAX System V Release 2 Version 2, on 3B System V
Release 2 Version Whatever-Gives-You-Demand-Paging, on Vax 4.2BSD As
Modified By John Bruner, on Sun UNIX, on CCI Power 5/20 UNIX, on VAX/VMS,
and on lots of other implementations, either the address space is set up so
that the result of casting NULL into a pointer does not point to a mapped
portion of your address space or you can ask that the address space be so
set up.

Moral: use the language the way it's supposed to be used, and you're better
off.

	Guy Harris