malloc impossible?

Chris Torek chris at mimsy.UUCP
Fri Jan 13 12:52:39 AEST 1989


>In article <15427 at mimsy.UUCP> I wrote:
>>... I claim that, if the requirement for malloc() were dropped, one
>>could implement C-minus-malloc on [more kinds of machines than now possible].

In article <9351 at smoke.BRL.MIL> gwyn at smoke.BRL.MIL (Doug Gwyn ) writes:
>Assuming that integers could not also live in floating-point space,
>C would not be implementable on such a hypothetical architecture.
>Consider
>	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.

And the code generated would be `allocate one object in each space,
the double from F and the int from I' (that is, float and int spaces)
rather than `allocate one object'.  References to the union would
write both spaces; pointers to unions would be represented as pairs.
If we had a pointer to that union, the code generated for

	p->i = 123;

might be something like (in vaxish assembly code)

	movq	4(ifp),r0r1	| pick up both halves of `p'
		| r0 holds &p->i; r1 holds &p->d
	movl	$123,(r0:i)	| write 123 in integer region
	movd	$0d123.0,(r1:f)	| write 123.0 in float region

(The value written to the float region can almost be arbitrary, but
it seems `cute' to assume that a compiler for this peculiar machine
should convert the value being stored so as to wind up with the same
value in each field.  It makes the machine appear to use the same
bit patterns for integers and floating point...!  [Well, almost.])

Note that this implies that `void *' (and therefore `char *')
pointers would have to be 8 bytes wide, since union pointers
are actually pairs.

>All data object types in C must be able to live in the same kind of space.

Not in C-minus-malloc (and perhaps a few other functions I may have
forgotten).  I am fairly sure that the `core' part of the language
can get away without this.  varargs/stdarg does look hard.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list