Question of Ignorance

Guy Harris guy at sun.uucp
Sat May 18 17:04:12 AEST 1985


> 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;

"Why can't you?"  On a machine where conversion between "int *" and "char *"
wasn't just pasting a different sticker on the same bit string, what would
the above construct mean if "ptr" were an "int *"?  Would it mean "assign
the value generated by casting -1 to a 'char *' to 'ptr' by copying bits?"
If so, it might not be correct...

> I have wanted to do this at various times, when I don't want to have
> to always cast the argument to FREE and I don't ever want to reference
> freed data accidentally...

On a Sun, or a CCI Power 5/20 (or, I'll bet, on some other machines which
are 68000-based or 68010/68020-based but derived from a 68000-based machine),

	#define FREE(ptr) free(char *) ptr); ptr = 0;

will do just what you want; attempting to access virtual address 0 causes
all sorts of loud bangings and clangings from your process.  A simple set of
changes to 4.2BSD for the VAX were posted to cause the VAX to do the same
under 4.2BSD; under VMS, it does so as a matter of course, and under System
V Release 2 Version 2, you can ask to have it do so.  Stuffing -1 into the
pointer is inappropriate (and, on a machine like the PDP-11, isn't even
guaranteed to cause dereferencing of the pointer to fail); the fact that you
can't get a construct like the one suggested to pass the C compiler isn't a
bug, it's a feature.

	Guy Harris



More information about the Comp.lang.c mailing list