Re: Goto?!? (was Re: A89: fwrite bug located (and a bugfix))


[Prev][Next][Index][Thread]

Re: Goto?!? (was Re: A89: fwrite bug located (and a bugfix))





You'll have to ask Zeljko about the goto. I just took his function and
changed this:

if((*(char*)ptr++=fgetc(f))<0) goto exit;

to this:

if((*(unsigned char*)ptr++=fgetc(f))<0) goto exit;

All I can think of is the goto makes the code clearer. I jumped too when I
saw it, though.

Michael Cowart


>
> 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: References: