home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / c / 12443 < prev    next >
Encoding:
Text File  |  1992-08-17  |  1.7 KB  |  48 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!mcsun!Germany.EU.net!news.netmbx.de!zrz.tu-berlin.de!cs.tu-berlin.de!jutta
  3. From: jutta@opal.cs.tu-berlin.de (Jutta Degener)
  4. Subject: Re: Memory alignment of struct elements
  5. Message-ID: <1992Aug17.192444.928@cs.tu-berlin.de>
  6. Sender: news@cs.tu-berlin.de
  7. Organization: Techn. University of Berlin, Germany
  8. References: <1992Aug12.133132.10723@Princeton.EDU> <BsxCtz.598@unx.sas.com> <adam.714060134@mcrware>
  9. Date: Mon, 17 Aug 1992 19:24:44 GMT
  10. Lines: 36
  11.  
  12. adam@microware.com (Adam Goldberg) says he's been forced to do things like:
  13. > typedef struct PARM {
  14. >  char MemIn[4], MemIType, MemISize[2];
  15. >  char MemOut[4],MemOType, MemOSize[2];
  16. > };
  17. >
  18. > Then on each usage of, say 'MemOut', I've got to:
  19. >
  20. >   (char *)(p->MemOut)
  21. >
  22. > Ugly.
  23.  
  24. Ugly?  Wrong!  (One more point for the identity of truth and beauty.)
  25.  
  26. P->MemOut, when used in a value context like this, turns from an
  27. `array [4] of char' into the address of the array's first element,
  28. &array[0], a `pointer to char'.  From a type system viewpoint you
  29. do not need the cast.
  30.  
  31. What you actually want is not `p->MemOut', but `the value of the
  32. bytes in p->MemOut, were they the bytes of the memory representation
  33. of a properly aligned pointer to char'.  The only portable way to get
  34. this is something along the lines of
  35.  
  36.     char * cp;
  37.     *(char **)memcpy((char *)&cp, p->MemOut, sizeof(cp));
  38.  
  39. Of course you are free to inline the memcpy.  (So is the compiler.)
  40.  
  41. This kind of interface could be a job for a code-generator with a
  42. C-like specification language, similar to those that exist for RPC.
  43. A generated assembler (or nonportable C) routine could encode and
  44. decode the routine's parameters and results and call your C function
  45. in between.
  46.  
  47. --Jutta
  48.