Is an object made up of bytes?

Brett Galloway brett at wjvax.UUCP
Thu Jan 22 03:30:11 AEST 1987


In article <5536 at brl-smoke.ARPA> gwyn at brl-smoke.ARPA (Doug Gwyn ) writes:
>> The semantics of "(char *) &object" aren't guaranteed to produce anything
>> that can be safely dereferenced to access a char.
>> ... there may be "big endian" byte-addressed architectures where the
>> numeric address of a word is not the lowest-valued address of the bytes
>> within the word ...
>
>I occurs to me that X3J11 needs to add a guarantee that at least a cast
>to (void *) results in something representing the lowest-valued address
>of any byte in the object pointed at by whatever pointer is being
>converted; otherwise what good are the mem*() functions?
>
>Some of the issues raised by RMS are deeper than I at first realized.

I agree.  It seems odd, though, that (void *) and (char *) would behave
differently.  I know that there is a lot of existing code that assumes
that (char *) behaves this way.  This assumption is necessary because
(void *) doesn't exist, and bcopy (or memc?py) on (char *)&foo is too
useful.  Another example is writing data to a file -- how could one ever
write anything but a character string to a file?  For example,
to write an object
	short foo;
to a file, one must do something like
	fwrite((char *)&foo,sizeof(short),1,file)
at least in 4.2BSD.  In order to maintain this ability, it must be
possible to obtain the "numeric address of an object which is the
lowest-valued address of the bytes within the object."  One could make
(void *) this object, but that is still not correct -- fwrite needs to
dereference the pointer to the object to get characters, not "voids".
I suppose one could do (char *)(void *)(&foo), but that is ugly.
-- 
-------------
Brett Galloway
{pesnta,twg,ios,qubix,turtlevax,tymix,vecpyr,certes,isi}!wjvax!brett



More information about the Comp.lang.c mailing list