home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / std / c / 3347 < prev    next >
Encoding:
Text File  |  1993-01-08  |  1.8 KB  |  45 lines

  1. Path: sparky!uunet!utcsri!dgp.toronto.edu!flaps
  2. Newsgroups: comp.std.c
  3. From: flaps@dgp.toronto.edu (Alan J Rosenthal)
  4. Subject: Re: Struct hack one last time (one last time)
  5. Message-ID: <1993Jan8.162244.11855@jarvis.csri.toronto.edu>
  6. References: <1993Jan7.221207.13818@leland.Stanford.EDU> <1993Jan8.065947.29719@sq.sq.com> <1993Jan8.190840.11087@taumet.com>
  7. Date: 8 Jan 93 21:22:44 GMT
  8. Lines: 35
  9.  
  10. Struct hack several last times.
  11.  
  12. A simpler objection to Dave Eisen's scheme is to take the return value from a
  13. malloc and cast it to pointers to two different struct types, stored in
  14. different pointer variables, and then use them alternately just like a union.
  15. The number of bytes malloced should be the max of the "sizeof"s of the two
  16. struct types.
  17.  
  18. Chris Volpe's code which begins:
  19. >struct foo {double field[2];} *fp;
  20. >char bar[]="LongerThanSizeOfStructFoo";
  21. >fp = (struct foo *)bar;
  22. is already invalid because you can't cast "bar" to a struct type because of
  23. alignment rules.  But you could malloc some memory, strcpy that string into it,
  24. then cast it, which is kind of like my union-like example.
  25.  
  26. steve@taumet.com (Steve Clamage) writes:
  27. >    struct S {
  28. >        double a[2];
  29. >        double b;
  30. >    } s;
  31. >    s.a[2] = 1.0;
  32.  
  33. and compares it to the struct hack.
  34.  
  35. This is completely different than the struct hack for two reasons.
  36.  
  37. This would only be similar to the struct hack if "s" were malloced, rather than
  38. declared, and if the type of "a" were char.  Neither of these properties holds
  39. in your example.  Both of these properties were crucial to some methods of
  40. arguing the validity of the struct hack, and the second property is crucial to
  41. all of them.
  42.  
  43. As well, the interpretation ruling about declaring an int[3][4] and accessing
  44. a[0][7] applies to Mr Clamage's example, imho, so his quoted code is invalid.
  45.