home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / std / c / 2575 < prev    next >
Encoding:
Text File  |  1992-09-07  |  1.6 KB  |  44 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: <1992Sep5.165014.7366@sq.sq.com>
  6. Organization: SoftQuad Inc., Toronto, Canada
  7. References: <715547606snx@grendel.demon.co.uk> <Bu3FMw.II4@sneaky.lonestar.org>
  8. Date: Sat, 5 Sep 92 16:50:14 GMT
  9. Lines: 33
  10.  
  11. > > Does ANSI say that structure assignment is done by member-wise copy or
  12. > > by bit-wise copying?
  13.  
  14. (As I said earlier, I do not believe that the answer to this is clear.)
  15.  
  16. > How would a standard-conforming program tell the difference?
  17.  
  18. Since the bitwise copy is a superset of the member-wise copy, a conforming
  19. program might be able to tell the difference *only if* the standard
  20. requires bitwise copying and the implementation copies member-wise.
  21. If that *is* the case, the following program is (I hope) strictly con-
  22. forming, but prints "Failure!" on an implementation which has certain
  23. alignment properties and which does member-wise copying.
  24.  
  25.     #include <stdio.h>
  26.     #include <stdlib.h>
  27.     #include <string.h>
  28.     main() {
  29.         static struct {char a; long b; char c;} zeroes, *p;
  30.         p = malloc (sizeof *p); if (!p) return 1;
  31.         memset (p, 'X', sizeof p);
  32.         *p = zeroes;
  33.         if (memchr (p, 'X', sizeof p)) printf ("Failure!\n");
  34.         return 0;
  35.     }
  36.  
  37. Untested code, because I don't have an ANSI C compiler handy.
  38. -- 
  39. Mark Brader           "Doing the wrong thing is worse than doing nothing."
  40. SoftQuad Inc., Toronto     "Doing *anything* is worse than doing nothing!"
  41. utzoo!sq!msb, msb@sq.com             -- Lynn & Jay: YES, PRIME MINISTER
  42.  
  43. This article is in the public domain.
  44.