get size of malloc'd object

Gregory Smith greg at utcsri.UUCP
Sat Jun 28 01:32:17 AEST 1986


In article <1986Jun27.01:36:11.4839 at utcs.uucp> flaps at 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



More information about the Comp.unix mailing list