home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / std / c / 2587 < prev    next >
Encoding:
Text File  |  1992-09-08  |  5.3 KB  |  125 lines

  1. Newsgroups: comp.std.c
  2. Path: sparky!uunet!uunet.ca!wildcan!sq!msb
  3. From: msb@sq.sq.com (Mark Brader)
  4. Subject: Re: How is struct assignment defined (idle query)?
  5. Message-ID: <1992Sep8.082316.13903@sq.sq.com>
  6. Organization: SoftQuad Inc., Toronto, Canada
  7. References: <715547606snx@grendel.demon.co.uk> <Bu3FMw.II4@sneaky.lonestar.org> <19920906.095303.273@almaden.ibm.com>
  8. Date: Tue, 8 Sep 92 08:23:16 GMT
  9. Lines: 114
  10.  
  11. > While the standard does not specify explictly one way or the other, the
  12. > fact that the equality operator is not allowed on structs implies that
  13. > member-wise copy is certainly allowed.
  14.  
  15. No; it implies at most the intention that member-wise copy be allowed.
  16.  
  17. > That is, since
  18. >   struct1 = struct2;
  19. >   if ( struct1 == struct2 )
  20. >     /* blah blah blah */
  21. > is not permitted, it would seem to me that the Committee was concerned
  22. > that allowing such comparisons would force an implementation to maintain
  23. > the integrity of the padding, either by explicitly zeroing it or by only
  24. > doing bitwise copying.  I expect that they considered dictating
  25. > implementation specifics to be outside their baliwick, so they ducked the
  26. > issue by disallowing structure comparisons. ...
  27.  
  28. Not really.  I have looked up the Rationale on this (actually the May 1988
  29. draft of the Rationale, but I think the final one is the same).  In section
  30. 3.3.9, it says:
  31.  
  32. #  The Committee considered, on more than one occasion, permitting
  33. #  comparison of structures for equality.  Such proposals foundered
  34. #  on the problem of holes in structures.  A byte-wise comparison of
  35. #  two structures would require that the holes assuredly be set to
  36. #  zero so that all holes would compare equal, a difficult task for
  37. #  automatic or dynamically allocated variables.  (The possibility
  38. #  of union-type elements in a structure raises insuperable problems
  39. #  with this approach.)  Otherwise the implementation would have to
  40. #  be prepared to break a structure comparison into an arbitrary number
  41. #  of member comparisons; a seemingly simple expression could thus
  42. #  expand into a substantial stretch of code, which is contrary to the
  43. #  _spirit_of_C_.
  44.  
  45. This is about comparisons and not assignments, and there are some
  46. differences between these operations.  However, the spirit of the
  47. above passage might be applied to structure assignments, and it would
  48. then say "bitwise assignment is in the spirit of C, but the value of
  49. the holes (padding) isn't part of the value of the structure, so you
  50. could do member-wise assignment if you really want".
  51.  
  52. But note that it doesn't *say* anything about assignments; rather, it
  53. is concerned more with code like this:
  54.  
  55.     struct {char a; long b;} z, *p;
  56.     p = malloc (sizeof *p);
  57.     p->a = 'x'; p->b = 17;
  58.     z.a = 'x'; z.b = 17;
  59.     if (*p == z) ...
  60.  
  61. where the problem would be in maintaining equality of the padding even
  62. though the structs did *not* acquire their "equal" values by assignment
  63. of one from the other.
  64.  
  65.  
  66. > > Since the bitwise copy is a superset of the member-wise copy, a conforming
  67. > > program might be able to tell the difference *only if* the standard
  68. > > requires bitwise copying and the implementation copies member-wise.
  69. > > If that *is* the case, the following program is (I hope) strictly con-
  70. > > forming, but prints "Failure!" on an implementation which has certain
  71. > > alignment properties and which does member-wise copying.
  72. > >    main() {
  73. > >         static struct {char a; long b; char c;} zeroes, *p;
  74. > >         p = malloc (sizeof *p); if (!p) return 1;
  75. > >         memset (p, 'X', sizeof p);
  76. > >         *p = zeroes;
  77. > >         if (memchr (p, 'X', sizeof p)) printf ("Failure!\n");
  78. > >         return 0;
  79. > >    }
  80. > > Untested code, because I don't have an ANSI C compiler handy.
  81. >
  82. > This isn't strictly-conforming, since it relies on implementation-defined
  83. > behaviour (namely, if there is padding between structure members and what
  84. > that padding looks like (maybe your compiler explitly sets it all to
  85. > 'X')).
  86.  
  87. I made "zeroes" static to get guaranteed initialization, but this fails
  88. for the padding, since the initialization of static things as is if they
  89. were assigned 0, and a value can't be assigned to padding.  The statement
  90.  
  91.     memset (&zeroes, 0, sizeof zeroes);
  92.  
  93. should be inserted next to the other memset() call.  Other than this bug,
  94. which Chris Torek also pointed out in email, I believe the code *is*
  95. strictly conforming -- as I said, *if* the standard is interpreted as
  96. requiring bitwise structure assignment.  If run on an implementation
  97. that does memberwise assignment, then whether or not it detects this
  98. (supposed) violation of the standard depends on implementation-defined
  99. behavior, but that does not make the program not strictly conforming.
  100.  
  101.  
  102. > But the point is moot since the Standard does not demand bitwise
  103. > assignment anyway.
  104.  
  105. Anyone feel like asking for an official ruling on that?
  106.  
  107. > Another case is IBM's C/370 compiler, which does *chunkwise* copy for
  108. > mixed-alignment assignments between packed and unpacked structures. ...
  109. > (This, of course, proves nothing ...
  110.  
  111. Because the concept of packed and unpacked versions of a structure is
  112. foreign to the standard in the first place.
  113.  
  114. > ... but it is an interesting case to consider).
  115.  
  116. Agreed.
  117. -- 
  118. Mark Brader, SoftQuad Inc., Toronto, utzoo!sq!msb, msb@sq.com
  119.     "I'm a little worried about the bug-eater," she said.  "We're embedded
  120.     in bugs, have you noticed?"        -- Niven, "The Integral Trees"
  121.  
  122. This article is in the public domain.
  123.