home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / std / c / 3393 < prev    next >
Encoding:
Text File  |  1993-01-21  |  3.5 KB  |  82 lines

  1. Newsgroups: comp.std.c
  2. Path: sparky!uunet!elroy.jpl.nasa.gov!ames!haven.umd.edu!decuac!pa.dec.com!engage.pko.dec.com!nntpd.lkg.dec.com!jit533.jit.dec.com!diamond
  3. From: diamond@jit533.jit.dec.com (Norman Diamond)
  4. Subject: Re: Is this allowed in C?
  5. Message-ID: <1993Jan22.012502.5241@nntpd.lkg.dec.com>
  6. Sender: usenet@nntpd.lkg.dec.com (USENET News System)
  7. Reply-To: diamond@jit.dec.com (Norman Diamond)
  8. Organization: Digital Equipment Corporation Japan , Tokyo
  9. References: <C17p4n.Dx8@undergrad.math.waterloo.edu>
  10. Date: Fri, 22 Jan 1993 01:25:02 GMT
  11. Lines: 69
  12.  
  13. In article <C17p4n.Dx8@undergrad.math.waterloo.edu> bbobak@undergrad.math.waterloo.edu (Brad Bobak) writes:
  14.  
  15. Well, I noticed a few years ago that most of the practical-oriented stuff
  16. moved to the new Computer Engineering department, but surely there should
  17. still be some consultants or TAs that could help with this kind of problem?
  18.  
  19. > I was wondering if the following was valid in C.
  20. > struct astruct { int something; int something_else; } blah;
  21. > main() {
  22. >    char *store;
  23. >    store = malloc(sizeof(blah));
  24. >    memcpy(store, &blah, sizeof(blah));
  25. >    /* how about this? */
  26. >    {
  27. >        int another_int = 5;
  28. >        memcpy(&((struct astruct *)store)->something, &another_int,
  29. >               sizeof(int));
  30. >    }
  31.  
  32. The C standard guarantees that whenever malloc() succeeds (which you forgot
  33. to check), the value will point to storage suitably aligned for everything,
  34. including structs.  So (struct astruct *)store can indeed be used as a pointer
  35. to a struct astruct.
  36.  
  37. Pedantically, the standard forgot to say that when pointers to objects are
  38. converted to pointers to void, it should be possible to use the pointers to
  39. void in calls to memcpy() and other such functions.  However, I don't think
  40. any implementor would dare to play games with this.
  41.  
  42. > and something else similar:
  43. >#define CHAR_MASK ((1 << BITS_IN_CHAR) - 1)
  44. >/* I realize this might fail if sizeof(int) == sizeof(char), if [...] */
  45.  
  46. This is indeed possible.  Why not use UCHAR_MAX?
  47.  
  48. > main() {
  49. >    int val;
  50. >    char *store;
  51. >    int ct;
  52. >    store = malloc(sizeof(val));
  53. >    for (ct=0; ct<sizeof(int); ct++)
  54. >        store[ct] = (val >> (ct * BITS_IN_CHAR)) & CHAR_MASK;
  55. >    for (val=0, ct=0; ct<sizeof(int); ct++)
  56. >        val |= store[ct] << (ct * BITS_IN_CHAR);
  57.  
  58. Looks reasonable to me.
  59.  
  60. > The reason I'm doing it this way is that I've made a memory allocation
  61. >  library. And some systems require special alignment of data. So to
  62. >  archive something, I have to copy it to a char array.
  63.  
  64. You mean that you might write the archive to a file and read it back on
  65. another system?  What happens if you write the file on a system with
  66. 8-bit chars and read it back on a system with 9-bit chars?
  67.  
  68. >This next question probably has surfaced a few times, but is there any way
  69. >to guarantee the right alignment of arbritary data on various architectures?
  70.  
  71. Whenever you declare a variable (an int or a struct of some sort or whatever)
  72. the implementation has to be able to handle it.  If the implementation fails
  73. to align it properly, then the implementation has to work it out somehow,
  74. generating extra slow code if necessary.  And when malloc() succeeds, it
  75. returns a pointer that you're allowed to use for any type of object.  However,
  76. if you want some implementation to provide alignments that will work for some
  77. different implementation, you're usually out of luck.
  78. --
  79. Norman Diamond                diamond@jit.dec.com
  80. If this were the company's opinion, I wouldn't be allowed to post it.
  81. Pardon me?  Or do I have to commit a crime first?
  82.