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

  1. Newsgroups: comp.std.c
  2. Path: sparky!uunet!cs.utexas.edu!uwm.edu!spool.mu.edu!torn!watserv2.uwaterloo.ca!watmath!undergrad.math.waterloo.edu!bbobak
  3. From: bbobak@undergrad.math.waterloo.edu (Brad Bobak)
  4. Subject: Re: Is this allowed in C?
  5. Message-ID: <C19Juw.7GL@undergrad.math.waterloo.edu>
  6. Organization: University of Waterloo
  7. References: <C17p4n.Dx8@undergrad.math.waterloo.edu> <1993Jan22.012502.5241@nntpd.lkg.dec.com>
  8. Date: Fri, 22 Jan 1993 16:16:07 GMT
  9. Lines: 87
  10.  
  11. In article <1993Jan22.012502.5241@nntpd.lkg.dec.com>, diamond@jit533.jit.dec.com (Norman Diamond) writes:
  12. > In article <C17p4n.Dx8@undergrad.math.waterloo.edu> bbobak@undergrad.math.waterloo.edu (Brad Bobak) writes:
  13. > Well, I noticed a few years ago that most of the practical-oriented stuff
  14. > moved to the new Computer Engineering department, but surely there should
  15. > still be some consultants or TAs that could help with this kind of problem?
  16. > > I was wondering if the following was valid in C.
  17. > > struct astruct { int something; int something_else; } blah;
  18. > > main() {
  19. > >    char *store;
  20. > >    store = malloc(sizeof(blah));
  21. > >    memcpy(store, &blah, sizeof(blah));
  22. > >    /* how about this? */
  23. > >    {
  24. > >        int another_int = 5;
  25. > >        memcpy(&((struct astruct *)store)->something, &another_int,
  26. > >               sizeof(int));
  27. > >    }
  28. > The C standard guarantees that whenever malloc() succeeds (which you forgot
  29. > to check), the value will point to storage suitably aligned for everything,
  30. > including structs.  So (struct astruct *)store can indeed be used as a pointer
  31. > to a struct astruct.
  32.  Yes, I didn't check malloc because this was just an example. But I always
  33.   do.
  34.  My example wasn't clear on what I really meant. Lets change the malloc()
  35.   line to:
  36.  store = malloc(sizeof(blah)+3) + 3;
  37.  /* so that store isn't guaranteed to be aligned for anything, except
  38.   * chars (is this true?)
  39.   */
  40. > Pedantically, the standard forgot to say that when pointers to objects are
  41. > converted to pointers to void, it should be possible to use the pointers to
  42. > void in calls to memcpy() and other such functions.  However, I don't think
  43. > any implementor would dare to play games with this.
  44. > > and something else similar:
  45. > >#define CHAR_MASK ((1 << BITS_IN_CHAR) - 1)
  46. > >/* I realize this might fail if sizeof(int) == sizeof(char), if [...] */
  47. > This is indeed possible.  Why not use UCHAR_MAX?
  48.  No reason. I will do this.
  49. > > main() {
  50. > >    int val;
  51. > >    char *store;
  52. > >    int ct;
  53. > >    store = malloc(sizeof(val));
  54. > >    for (ct=0; ct<sizeof(int); ct++)
  55. > >        store[ct] = (val >> (ct * BITS_IN_CHAR)) & CHAR_MASK;
  56. > >    for (val=0, ct=0; ct<sizeof(int); ct++)
  57. > >        val |= store[ct] << (ct * BITS_IN_CHAR);
  58. > Looks reasonable to me.
  59. > > The reason I'm doing it this way is that I've made a memory allocation
  60. > >  library. And some systems require special alignment of data. So to
  61. > >  archive something, I have to copy it to a char array.
  62. > You mean that you might write the archive to a file and read it back on
  63. > another system?  What happens if you write the file on a system with
  64. > 8-bit chars and read it back on a system with 9-bit chars?
  65.  No, actually I mean to just archive something in memory.
  66.  When sending data across the network, I convert everything to bitstreams
  67.   and use a fixed size for chars/ints (if a certain architecture can't
  68.   support that size, my library will say so)
  69. > >This next question probably has surfaced a few times, but is there any way
  70. > >to guarantee the right alignment of arbritary data on various architectures?
  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.  I thought this would be the case. Guess I'll have to stick to the slow way.
  79. > --
  80. > Norman Diamond                diamond@jit.dec.com
  81. > If this were the company's opinion, I wouldn't be allowed to post it.
  82. > Pardon me?  Or do I have to commit a crime first?
  83.  
  84.  Sorry about the length of the article, but I don't really see what I can
  85.   cut out.
  86.