🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #1166

Re: How is struct assignment defined (idle query)?

#1166
From: Mark Brader
Date: Sat, 05 Sep 1992 16:50
33 lines
1666 bytes
> > Does ANSI say that structure assignment is done by member-wise copy or
> > by bit-wise copying?

(As I said earlier, I do not believe that the answer to this is clear.)

> How would a standard-conforming program tell the difference?

Since the bitwise copy is a superset of the member-wise copy, a conforming
program might be able to tell the difference *only if* the standard
requires bitwise copying and the implementation copies member-wise.
If that *is* the case, the following program is (I hope) strictly con-
forming, but prints "Failure!" on an implementation which has certain
alignment properties and which does member-wise copying.

	#include <stdio.h>
	#include <stdlib.h>
	#include <string.h>
	main() {
		static struct {char a; long b; char c;} zeroes, *p;
		p = malloc (sizeof *p); if (!p) return 1;
		memset (p, 'X', sizeof p);
		*p = zeroes;
		if (memchr (p, 'X', sizeof p)) printf ("Failure!\n");
		return 0;
	}

Untested code, because I don't have an ANSI C compiler handy.
-- 
Mark Brader		   "Doing the wrong thing is worse than doing nothing."
SoftQuad Inc., Toronto     "Doing *anything* is worse than doing nothing!"
utzoo!sq!msb, msb@sq.com		     -- Lynn & Jay: YES, PRIME MINISTER

This article is in the public domain.
Technical Details
Message-ID: <1992Sep5.165014.7366@sq.sq.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!uunet.ca!wildcan!sq!msb
References: <715547606snx@grendel.demon.co.uk> <Bu3FMw.II4@sneaky.lonestar.org>

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c
Path: arc1-ca.pugleaf.net!sparky!uunet!uunet.ca!wildcan!sq!msb
From: msb@sq.sq.com (Mark Brader)
Subject: Re: How is struct assignment defined (idle query)?
Message-ID: <1992Sep5.165014.7366@sq.sq.com>
Organization: SoftQuad Inc., Toronto, Canada
References: <715547606snx@grendel.demon.co.uk> <Bu3FMw.II4@sneaky.lonestar.org>
Date: Sat, 5 Sep 92 16:50:14 GMT
Lines: 33