Path: utzoo!attcan!uunet!mcvax!dik From: dik@cwi.nl (Dik T. Winter) Newsgroups: comp.lang.c Subject: Re: malloc impossible? Message-ID: <7821@boring.cwi.nl> Date: 12 Jan 89 23:13:30 GMT References: <19@xenlink.UUCP> <225800106@uxe.cso.uiuc.edu> <310@twwells.uucp> <9342@smoke.BRL.MIL> <15427@mimsy.UUCP> <9351@smoke.BRL.MIL> <27499@ucbvax.BERKELEY.EDU> Organization: CWI, Amsterdam Lines: 41 Considering a machine with separate int and float space: Doug Gwyn (VLD/VMB) writes: > union { > double d; > int i; > } u; > u.d = 123.0; > printf("%d\n", u.i); >This is required to work, although the specific value printed of course >depends on details of numeric representation on the specific system. >All data object types in C must be able to live in the same kind of space. > Jim Shankland answers: > Well, this is getting pretty esoteric, I suppose, but if I were implementing > (C - malloc) on such a machine, couldn't I reserve space for this union > in both fp space and integer space? The value ouput by the printf above > would, of course, be entirely unaffected by the preceding assignment; but > as you say, the value printed is system-specific, anyway. In other words, > are unions *required* to overlay their members onto the same physical > storage? > Still more esoteric: More is required depending on ANSI C requirements. For instance should *((double *) (&u)) be identical to u.d? And what about *((double *) (int *) (&u)) I think the latter is not required; but if it is the system will have problems. The system will also have problems if you cast through void*; which is required to work I think. An easy solution is to allocate space (through malloc or through local variables) in both fp and integer space, and take the proper space depending on context. But this implies that malloc is implementable (at a cost). -- dik t. winter, cwi, amsterdam, nederland INTERNET : dik@cwi.nl BITNET/EARN: dik@mcvax