Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site utcsri.UUCP Path: utzoo!utcsri!greg From: greg@utcsri.UUCP (Gregory Smith) Newsgroups: net.unix,net.unix-wizards,net.arch,net.lang.c Subject: Re: get size of malloc'd object Message-ID: <3025@utcsri.UUCP> Date: Fri, 27-Jun-86 11:32:17 EDT Article-I.D.: utcsri.3025 Posted: Fri Jun 27 11:32:17 1986 Date-Received: Fri, 27-Jun-86 11:57:37 EDT References: <165@daisy.UUCP> <334@valid.UUCP> <2206@peora.UUCP> <2081@umcp-cs.UUCP> <2216@peora.UUCP> <1986Jun27.01:36:11.4839@utcs.uucp> Reply-To: greg@utcsri.UUCP (Gregory Smith) Organization: CSRI, University of Toronto Lines: 55 Keywords: Yet another PCC bug Summary: How to get most strict alignment, and YAPCCbug In article <1986Jun27.01:36:11.4839@utcs.uucp> flaps@utcs.UUCP (Alan J Rosenthal) writes: > >A way to get a sufficient alignment constant should be as follows: > >union ALIGNED { > char garbage0; > unsigned char garbage1; > short garbage2; > int garbage3; > unsigned garbage4; > long garbage5; > unsigned long garbage6; > float garbage7; > double garbage8; >}; > >(sorry if I've left out any) > >Then you can use sizeof(ALIGNED) as the alignment constant. Am I mistaken? ^ union >The problem is that this number might be greater than the actual >alignment constant, for example if a "double" has length 8 but only >needs quad-word alignment. > How about: struct foo{ char ignatz; union ALIGNED wumpus; /* union ALIGNED as above */ }; then the most strict alignment is given by sizeof( struct foo ) - sizeof( union ALIGNED ) Interesting aside: I first thought of declaring 'struct foo xyzzy' and using: ((char *)&xyzzy.wumpus - (char*)&xyzzy) The problem is that this is not a run time constant, but it could be if the semantics of '-' in constant expressions allowed pointer subtraction if both pointers were based at the same label. The portability of this is dubious. The interesting part: int i = ((char *)&xyzzy.wumpus - (char*)&xyzzy); this gave the following on PCC: "test.c", line 20: compiler error: expression causes compiler loop: try simplifying -- "Shades of scorpions! Daedalus has vanished ..... Great Zeus, my ring!" ---------------------------------------------------------------------- Greg Smith University of Toronto UUCP: ..utzoo!utcsri!greg