Goto?!? (was Re: A89: fwrite bug located (and a bugfix))
[Prev][Next][Index][Thread]
Goto?!? (was Re: A89: fwrite bug located (and a bugfix))
> #undef fread // this will cancel old definition from stdio.h
> unsigned fread(void *ptr,unsigned size,unsigned n,FILE *f)
> {
> unsigned i,j;
> int saveflags=f->flags;
> f->flags|=_F_BIN;
> for(i=0;i<n;i++)
> for(j=0;j<size;j++)
> if((*(unsigned char*)ptr++=fgetc(f))<0) goto exit;
> exit:
> f->flags=saveflags;
> return i;
> }
Somebody PLEASE tell me why you're using _GOTO_ there before Wing and I die
of laughter?!?
Geez, you're breaking the ultimate C taboo here - the only accepted use of
goto involves breaking out of nested loops with faster/smaller code, but
that doesn't apply in this instance - I can do it without goto, and I bet
it'll generate the same code.
Why not use the following code? I think this meets all conditions, is
anyone smart enough to check and see?
It's a question of whether or not this will generate the same code - a good
test of how smart GCC is. Can it optimize this code into the equivalent
goto'd form, without using a dreaded goto? hmmm. . .
#undef fread // this will cancel old definition from stdio.h
unsigned fread(void *ptr,unsigned size,unsigned n,FILE *f)
{
unsigned i=0,j=0;
int saveflags=f->flags;
f->flags|=_F_BIN;
while (i++<n && (*(unsigned char*)ptr++=fgetc(f))>=0)
while (j++<size && (*(unsigned char*)ptr++=fgetc(f))>=0)
continue;
f->flags=saveflags;
return i;
}
As confusing as this is - it could probably use some extra parentheses - I'm
nearly sure it works. Can anyone find out if GCC is good enough to have
this output the proper code???
-Scott
Follow-Ups: