🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #694

Do functions always return copies?

#694
From: Paul Eggert
Date: Wed, 11 Oct 1989 19:30
52 lines
1672 bytes
I thought that in a function yielding a non-void type T, the statement

	return E;

is equivalent to the statement

	{ T temp = (E);  return temp; }

But the following program has me wondering whether this is true in ANSI C.
With indirection() as defined below, shouldn't ``u.s2.s = indirection(&u.s1.s);''
yield a _copy_ of u.s1.s, and assign that copy to u.s2.s?  Because of the
implicit copy, doesn't this example satisfy the overlap restriction of pANS
3.3.16.1?

I'm asking because GCC disagrees with me on a Sun-3 unless I add the flag
-fpcc-struct-return; it outputs ``1, 65538, 131075, 196612 should be 1, 2, 3,
4'' instead of ``1, 2, 3, 4 should be 1, 2, 3, 4''.  Replacing ``return *p;''
as described above fixes the problem.  Shouldn't this be a bug in GCC?

union u {
	struct s1 {
		struct s {
			int i, j, k, l;
		} s;
		char c;
	} s1;
	struct s2 {
		char c;
		struct s s;
	} s2;
};

struct s indirection(p)
	struct s *p;
{
	return *p;
}

int main()
{
	union u u;
	u.s1.s.i = 1;
	u.s1.s.j = 2;
	u.s1.s.k = 3;
	u.s1.s.l = 4;
	u.s2.s = indirection(&u.s1.s);
	printf(
		"%d, %d, %d, %d should be 1, 2, 3, 4\n",
		u.s2.s.i, u.s2.s.j, u.s2.s.k, u.s2.s.l
	);
	return 0;
}
Technical Details
Message-ID: <37@looney.twinsun.com>
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!gem.mps.ohio-state.edu!usc!ucla-cs!twinsun!rise.twinsun.com!eggert

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!gem.mps.ohio-state.edu!usc!ucla-cs!twinsun!rise.twinsun.com!eggert
From: eggert@rise.twinsun.com (Paul Eggert)
Newsgroups: comp.std.c
Subject: Do functions always return copies?
Message-ID: <37@looney.twinsun.com>
Date: 11 Oct 89 19:30:11 GMT
Sender: news@twinsun.com
Reply-To: eggert@twinsun.com (Paul Eggert)
Organization: Twin Sun, Inc
Lines: 52