Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84 SMI; site sun.uucp Path: utzoo!linus!philabs!prls!amdimage!amdcad!decwrl!sun!guy From: guy@sun.uucp (Guy Harris) Newsgroups: net.lang.c Subject: Re: Re: Question of Ignorance Message-ID: <2190@sun.uucp> Date: Sat, 18-May-85 03:04:12 EDT Article-I.D.: sun.2190 Posted: Sat May 18 03:04:12 1985 Date-Received: Mon, 20-May-85 04:22:49 EDT References: <198@unccvax.UUCP>, <1427@bbncca.ARPA> <5590@utzoo.UUCP> <480@bbnccv.UUCP> <248@ucbcad.UUCP> Organization: Sun Microsystems, Inc. Lines: 33 > 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