PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Re: Goto still considered ful
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Re: Goto still considered ful

Réponse
 
LinkBack Outils de la discussion
Vieux 19/10/2007, 05h56   #1
Ben Pfaff
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Goto still considered ful

CBFalconer <cbfalconer@yahoo.com> writes:

> int fcopy(char const *dst, char const *src) {
> FILE *sf, *df;
> int ch
>
> if (sf = fopen(src, "r") { /* text files */
> if (df = fopen(dst, "w") {
> while (EOF != (ch = getc(sf))) putc(ch, df);
> fclose(df);
> }
> fclose(sf);
> }
> return sf && df; /* non-zero for success */


I think that this is a bad idea: it is a lot like checking the
value of a pointer after it has been freed (often fclose will
actually call free on the stream, in fact). I'm sure it works in
practice on almost every implementation though.

> }


--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
  Réponse avec citation
Vieux 19/10/2007, 07h29   #2
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Goto still considered ful

Ben Pfaff <blp@cs.stanford.edu> writes:
> CBFalconer <cbfalconer@yahoo.com> writes:
>> int fcopy(char const *dst, char const *src) {
>> FILE *sf, *df;
>> int ch
>>
>> if (sf = fopen(src, "r") { /* text files */
>> if (df = fopen(dst, "w") {
>> while (EOF != (ch = getc(sf))) putc(ch, df);
>> fclose(df);
>> }
>> fclose(sf);
>> }
>> return sf && df; /* non-zero for success */

>
> I think that this is a bad idea: it is a lot like checking the
> value of a pointer after it has been freed (often fclose will
> actually call free on the stream, in fact). I'm sure it works in
> practice on almost every implementation though.
>
>> }


If the first fopen() fails, no value is assigned to df, and accessing
it invokes undefined behavior.

In addition, I think you're write; though fclose() cannot change the
value of of its argument, it can cause it to become indeterminate.
(In practice, it will probably appear to be be null or non-null after
fclose() if it was, respectively, null or non-null before fclose().)

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  Réponse avec citation
Vieux 19/10/2007, 08h36   #3
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Goto still considered ful

Keith Thompson <kst-u@mib.org> writes:
[...]
> In addition, I think you're write;

[...]

Did I really write "write" instead of "right"? Sigh.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  Réponse avec citation
Vieux 19/10/2007, 10h27   #4
Flash Gordon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Goto still considered ful

Keith Thompson wrote, On 19/10/07 07:29:
> Ben Pfaff <blp@cs.stanford.edu> writes:
>> CBFalconer <cbfalconer@yahoo.com> writes:
>>> int fcopy(char const *dst, char const *src) {
>>> FILE *sf, *df;
>>> int ch
>>>
>>> if (sf = fopen(src, "r") { /* text files */
>>> if (df = fopen(dst, "w") {
>>> while (EOF != (ch = getc(sf))) putc(ch, df);
>>> fclose(df);
>>> }
>>> fclose(sf);
>>> }
>>> return sf && df; /* non-zero for success */

>> I think that this is a bad idea: it is a lot like checking the
>> value of a pointer after it has been freed (often fclose will
>> actually call free on the stream, in fact). I'm sure it works in
>> practice on almost every implementation though.
>>
>>> }

>
> If the first fopen() fails, no value is assigned to df, and accessing
> it invokes undefined behavior.


If the first fopen fails sf will be a null pointer and short circuit
evaluation ensures that df is not accessed, so it is OK.

> In addition, I think you're write; though fclose() cannot change the
> value of of its argument, it can cause it to become indeterminate.
> (In practice, it will probably appear to be be null or non-null after
> fclose() if it was, respectively, null or non-null before fclose().)


Agreed.
--
Flash Gordon
  Réponse avec citation
Vieux 19/10/2007, 11h49   #5
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Goto still considered ful

Flash Gordon <spam@flash-gordon.me.uk> writes:
> Keith Thompson wrote, On 19/10/07 07:29:
>> Ben Pfaff <blp@cs.stanford.edu> writes:
>>> CBFalconer <cbfalconer@yahoo.com> writes:
>>>> int fcopy(char const *dst, char const *src) {
>>>> FILE *sf, *df;
>>>> int ch
>>>>
>>>> if (sf = fopen(src, "r") { /* text files */
>>>> if (df = fopen(dst, "w") {
>>>> while (EOF != (ch = getc(sf))) putc(ch, df);
>>>> fclose(df);
>>>> }
>>>> fclose(sf);
>>>> }
>>>> return sf && df; /* non-zero for success */
>>> I think that this is a bad idea: it is a lot like checking the
>>> value of a pointer after it has been freed (often fclose will
>>> actually call free on the stream, in fact). I'm sure it works in
>>> practice on almost every implementation though.
>>>
>>>> }

>> If the first fopen() fails, no value is assigned to df, and accessing
>> it invokes undefined behavior.

>
> If the first fopen fails sf will be a null pointer and short circuit
> evaluation ensures that df is not accessed, so it is OK.

[...]

You're right.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  Réponse avec citation
Vieux 19/10/2007, 14h50   #6
Ben Bacarisse
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Goto still considered ful

Keith Thompson <kst-u@mib.org> writes:

> Flash Gordon <spam@flash-gordon.me.uk> writes:
>> Keith Thompson wrote, On 19/10/07 07:29:
>>> Ben Pfaff <blp@cs.stanford.edu> writes:
>>>> CBFalconer <cbfalconer@yahoo.com> writes:

<snip>
>>>>> if (sf = fopen(src, "r") { /* text files */

<snip>
>>>> I think that this is a bad idea:

<snip>
>>>>> }
>>> If the first fopen() fails, no value is assigned to df, and accessing
>>> it invokes undefined behavior.

>>
>> If the first fopen fails sf will be a null pointer and short circuit
>> evaluation ensures that df is not accessed, so it is OK.

>
> You're right.


....but it is still a syntax error!

--
Ben.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h12.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12347 seconds with 14 queries