Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rutgers!mtune!codas!peora!ucf-cs!usfvax2!chips From: chips@usfvax2.UUCP (Chip Salzenberg) Newsgroups: comp.lang.c Subject: Re: How big is a (??? *) Message-ID: <767@usfvax2.UUCP> Date: Fri, 22-May-87 13:10:11 EDT Article-I.D.: usfvax2.767 Posted: Fri May 22 13:10:11 1987 Date-Received: Sat, 23-May-87 17:28:24 EDT References: <737@edge.UUCP> Organization: AT Engineering, Tampa, FL Lines: 35 Summary: Use void * instead of char * Endurium: 120 cubic meters Missile-Launcher: Class 5 In article <737@edge.UUCP>, doug@edge.UUCP (Doug Pardee) writes: > To the "outside world", each object is known only by a handle, which is > typically (here it comes, folks) a pointer to that malloc'ed struct. > [...] > Possibility #1: We cavalierly declare that no pointers are longer than > char pointers: Does the word "assume" ring a bell? > Possibility #2: We cavalierly declare that all pointers to pointers are > the same length: Again, an unsafe assumption. If, for example, "char *" is bigger than "int *", then "char *" may have more stringent alignment requirements than "int *". Therefore, "char **" could be smaller than "int **". (Any resemblance to real architectures, living or dead, is purely coincidental.) > Possibility #3: We observe that malloc is declared as "char *", and > so we assume that all pointers returned by malloc are truly (char *), > even if we subsequently cast them to (struct foo *) and put a struct foo > into that memory. Use "void *" instead. Void pointers are guaranteed to hold any pointer value without losing precision. Cast the return value of malloc() to "void *" and use the resulting void pointer as a handle. If you want your code to be portable to compilers lacking a "void" type, make a typedef (typedef void *HANDLE) so that you can conveniently change your mind. -- Chip Salzenberg Address: "{gatech,cbatt,akgua}!usfvax2!ateng!chip" AT Engineering, Tampa, FL Redress: "chips@usfvax2.UUCP" "Use the Source, Luke!" My opinions do not necessarily agree with anything.