Article View: comp.unix.programmer
Article #3668Re: gcc -O2 bug
From: Peter Seebach
Date: Thu, 08 Aug 1996 00:00
Date: Thu, 08 Aug 1996 00:00
58 lines
2888 bytes
2888 bytes
In article <vkn3059yhu.fsf@atari.connectinc.com>,
Bryan Ischo <bji@connectinc.com> wrote:
>In article <4udd3i$90k@solutions.solon.com> seebs@solutions.solon.com
>(Peter Seebach) writes:
>C arguments are passed in by value, not by reference. So, if I write a
>function which takes an int as an argument, then the function will NOT be able
>to modify the "original" value. For example:
Correct.
>Now, the only way for me to write and use foo() with the same signature which
>modifies b is to do something like this:
Nope...
>Note that this REQUIRES that sizeof(int) == sizeof(int *). Not a very robust
>programming practice.
It certainly isn't. But consider:
extern int *gptr;
void foo(int a) {
*gptr += 1;
}
void bar(void) {
int b = 1;
gptr = &b;
foo(a);
}
Neat, huh?
>True, but &flag was not passed in to printf in the original code. What I am
>getting at is, 'flag' in the original code was only ever used as an integer
>and was only ever passed by value, so it could not be modified. Maybe
>compilers aren't written to cover rules as specific as this, but it would be
>possible. I mean, the whole notion of "volatile" is complicated enough to
>require similarly convoluted optimization logic.
The main problem is that, while looking inside the loop code, you can't be
sure *no one* has gotten the address of flag. Or rather, you can, but the
compiler doesn't, so it assumes that any external function may have *side
effects* which modify flag.
>And I was just asking for confirmation of this idea.
Your understanding is correct; you are merely failing to account for the
idiocy of programmers, which most optimizers choose to accept rather than
second-guessing.
-s
--
Peter Seebach - seebs@solon.com - Copyright 1996 - http://www.solon.com/~seebs
Unix/C Wizard - send mail for help, or send money for consulting!
The *other* C FAQ, the hacker FAQ, et al. See web page above.
Unsolicited email (junk mail and ads) is unwelcome, and will be billed for.
Technical Details
Message-ID:
<4ue9bu$phd@solutions.solon.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<4ubnat$75u@freeside.fc.net> <vkohklaizj.fsf@atari.connectinc.com> <4udd3i$90k@solutions.solon.com> <vkn3059yhu.fsf@atari.connectinc.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 11318c,7d30cae02ba9b089 X-Google-Attributes: gid11318c,public X-Google-Thread: 10c696,7d30cae02ba9b089 X-Google-Attributes: gid10c696,public X-Google-Thread: fc89c,7d30cae02ba9b089 X-Google-Attributes: gidfc89c,public X-Google-Thread: 1014db,7d30cae02ba9b089 X-Google-Attributes: gid1014db,public From: seebs@solutions.solon.com (Peter Seebach) Subject: Re: gcc -O2 bug Date: 1996/08/08 Message-ID: <4ue9bu$phd@solutions.solon.com> X-Deja-AN: 173039954 references: <4ubnat$75u@freeside.fc.net> <vkohklaizj.fsf@atari.connectinc.com> <4udd3i$90k@solutions.solon.com> <vkn3059yhu.fsf@atari.connectinc.com> organization: Usenet Fact Police (Undercover) reply-to: seebs@solon.com newsgroups: comp.unix.programmer,gnu.gcc.help,comp.os.linux.development.apps,comp.lang.c