Article View: comp.std.c
Article #3347Re: Struct hack one last time (one last time)
From: Alan J Rosenthal
Date: Fri, 08 Jan 1993 21:22
Date: Fri, 08 Jan 1993 21:22
35 lines
1844 bytes
1844 bytes
Struct hack several last times.
A simpler objection to Dave Eisen's scheme is to take the return value from a
malloc and cast it to pointers to two different struct types, stored in
different pointer variables, and then use them alternately just like a union.
The number of bytes malloced should be the max of the "sizeof"s of the two
struct types.
Chris Volpe's code which begins:
>struct foo {double field[2];} *fp;
>char bar[]="LongerThanSizeOfStructFoo";
>fp = (struct foo *)bar;
is already invalid because you can't cast "bar" to a struct type because of
alignment rules. But you could malloc some memory, strcpy that string into it,
then cast it, which is kind of like my union-like example.
steve@taumet.com (Steve Clamage) writes:
> struct S {
> double a[2];
> double b;
> } s;
> s.a[2] = 1.0;
and compares it to the struct hack.
This is completely different than the struct hack for two reasons.
This would only be similar to the struct hack if "s" were malloced, rather than
declared, and if the type of "a" were char. Neither of these properties holds
in your example. Both of these properties were crucial to some methods of
arguing the validity of the struct hack, and the second property is crucial to
all of them.
As well, the interpretation ruling about declaring an int[3][4] and accessing
a[0][7] applies to Mr Clamage's example, imho, so his quoted code is invalid.
Technical Details
Message-ID:
<1993Jan8.162244.11855@jarvis.csri.toronto.edu>Path:
arc1-ca.pugleaf.net!sparky!uunet!utcsri!dgp.toronto.edu!flapsReferences:
<1993Jan7.221207.13818@leland.Stanford.EDU> <1993Jan8.065947.29719@sq.sq.com> <1993Jan8.190840.11087@taumet.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!utcsri!dgp.toronto.edu!flaps Newsgroups: comp.std.c From: flaps@dgp.toronto.edu (Alan J Rosenthal) Subject: Re: Struct hack one last time (one last time) Message-ID: <1993Jan8.162244.11855@jarvis.csri.toronto.edu> References: <1993Jan7.221207.13818@leland.Stanford.EDU> <1993Jan8.065947.29719@sq.sq.com> <1993Jan8.190840.11087@taumet.com> Date: 8 Jan 93 21:22:44 GMT Lines: 35