malloc impossible?
Dik T. Winter
dik at cwi.nl
Fri Jan 13 09:13:30 AEST 1989
Considering a machine with separate int and float space:
Doug Gwyn (VLD/VMB) <gwyn> 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 at cwi.nl
BITNET/EARN: dik at mcvax
More information about the Comp.lang.c
mailing list