lkml.org 
[lkml]   [2000]   [Oct]   [17]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
SubjectRe: Patch to remove undefined C code
From
Date
Richard Guenther <richard.guenther@student.uni-tuebingen.de> writes:

> On Tue, 17 Oct 2000, Bernd Schmidt wrote:
>
> > On Tue, 17 Oct 2000, Richard Guenther wrote:
> >
> > > On Mon, 16 Oct 2000, Bernd Schmidt wrote:
> > >
> > > > I've been playing with some gcc patches to detect code with undefined
> > > > behaviour of the i = i++ variety. The patch below fixes all places in
> > > > the kernel that I could find. Note that in some cases, it wasn't
> > > > entirely clear what the code intended, so I had to guess.
> > > >
> > > > I haven't tested this patch at all other than to make sure it compiles.
> > >
> > > The following one is wrong, tho - should be rather
> > > str[i] = dn[i]; i++;
> >
> > Nope. (Well, at least you need to add extra braces.) The comma is a
> > sequence point.
>
> Umm, I thought, dn[i], i++ evaluates to i and dn[i++] evaluates
> to dn[i], so it should be either i++, dn[i-1] or the one I showed
> above?

dn[i], i++ evaluates to i, but str[i] = dn[i], i++ sets str[i] to
dn[i] first, then increments i returning its previous value, which is
discarded. Which was probably specified this way so

for(i=1,j=2; something; something else)

works as expected, rather than setting i and j to 2, and discarding
the 1.

Just to encourage empiricism, I usually check stuff like that with

% cat > foo.c
main(){
int i;
i = 1 , 2;
printf("%d\n",i);
}
% gcc foo.c
% ./a.out
1

or similar if I'm confused.

- M

--
Mark "Monty" Montague | monty@gg.caltech.edu | I don't do Windows(tm)
I'm dubious about any company whose assets can be destroyed by rm -rf
<URL:http://www.gg.caltech.edu/~monty/monty.shtml>
X-PGP-Fingerprint: E4 EA 6D B1 82 46 DB A1 B0 FF 60 B9 F9 5D 5C F7
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
Please read the FAQ at http://www.tux.org/lkml/

\
 
 \ /
  Last update: 2005-03-22 12:41    [W:0.063 / U:1.980 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site