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

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!uunet.ca!geac!zooid!ross
  3. From: Ross Ridge <ross@zooid.guild.org>
  4. Subject: Re: Memory alignment of struct elements
  5. Organization: ZOOiD BBS
  6. Date: Mon, 17 Aug 1992 23:08:11 GMT
  7. Message-ID: <1992Aug17.230811.10813@zooid.guild.org>
  8. References: <1992Aug12.133132.10723@Princeton.EDU> <BsxCtz.598@unx.sas.com> <adam.714060134@mcrware>
  9. Lines: 57
  10.  
  11. adam@microware.com (Adam Goldberg) writes:
  12. >That's all well and good, but what if, say, you are coding an implementation
  13. >of LIM EMS 4.0, which takes as a parameter a structure which, in assembler,
  14. >looks like this:
  15. >
  16. >Parm Struct
  17. >MemIn      dd   ?   ; 4 bytes
  18. >MemIType   db   ?   ; 1 byte
  19. >MemISize   dw   ?   ; 2 bytes
  20. >MemOut     dd   ?   ; 4 bytes
  21. >MemOType   db   ?   ; 1 byte    
  22. >MemOSize   dw   ?   ; 2 bytes
  23. >Parm Struct
  24. >
  25. >In C, this *could* be :
  26. >
  27. >typedef struct PARM {
  28. >  char *MemIn, MemIType;
  29. >  short MemISize;
  30. >  char *MemOut, MemOType;
  31. >  short MemOSize;
  32. >} PARM, *pPARM;
  33. >
  34. >Except for the addition of superflourous padding bytes.  Is there any
  35. >standard way to FORCE the C Struct to be identical to the Asm struct?
  36.  
  37. No, of course not, on some machine architechures it's impossible 
  38. define a struct like yours without necessary, not superflourous, padding
  39. in assembly or not.  In fact there is no standard way specfiying
  40. a two byte wide member value, there is no guarantee that shorts are
  41. this size.  In fact even MS-DOS C compilers don't guarantee that the char
  42. pointers will be 4 bytes wide, compile this in the wrong mode and they'll
  43. be 2 bytes wide.  
  44.  
  45. >No, I didn't think so.  There ought to be, though.  I've been forced to
  46. >do nasty things like:
  47. >
  48. >typedef struct PARM {
  49. >  char MemIn[4], MemIType, MemISize[2];
  50. >  char MemOut[4],MemOType, MemOSize[2];
  51. >};
  52.  
  53. Why not do something "nasty" like this:
  54.  
  55.     #pragma pack(1)        /* for MSC */
  56.     #pragma option -a-    /* for Borland C */
  57.  
  58. Since you're defining an extreamly OS specific structure, you might
  59. as well use a compiler specific feature.
  60.  
  61.                             Ross Ridge
  62.  
  63. -- 
  64. Ross Ridge - The Great HTMU                         l/     //
  65.                                     [OO][oo]
  66. ross@zooid.guild.org                            /()\/()/
  67. uunet.ca!zooid!ross                             db     //
  68.