Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Dumb Question.

Reply
Thread Tools

Dumb Question.

 
 
vashwath@rediffmail.com
Guest
Posts: n/a
 
      12-16-2005
#include <stdio.h>

int main()
{
FILE *fp;
char s[100];

fopen("file.txt","w+");

fprintf(fp,"HI\n");

fclose(fp);
fscanf(fp,"%s",s);
printf("%s\n",s);
}

I know this will invoke undefined behaviour.Does undefined behavior
mean it MIGHT also work as expected in this case?Is it possible to read
from a file which already closed at least once out of 1000 times on
thousand different environment?

 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      12-16-2005
wrote:

> #include <stdio.h>
>
> int main()
> {
> FILE *fp;
> char s[100];
>
> fopen("file.txt","w+");


it's a good idea to check the return value of fopen(). It is an even
better idea to
initialise fp before you use it...

> fprintf(fp,"HI\n");
>
> fclose(fp);
> fscanf(fp,"%s",s);
> printf("%s\n",s);
> }
>
> I know this will invoke undefined behaviour.Does undefined behavior
> mean it MIGHT also work as expected in this case?


it really is a bad idea to expect anything from Undefined Behaviour.
Long
ago on this ng a poster said he had actually been asked by the computer

if he wanted to reformat C: after a particularly bad example of UB.

> Is it possible to read
> from a file which already closed at least once out of 1000 times on
> thousand different environment?


I suspect most platforms will not ley you write to a closed file. Why
would you
want to do this? You might survive sticking your fingers in a live
electrical socket,
but why do that?


--
Nick Keighley

 
Reply With Quote
 
 
 
 
Ingo Menger
Guest
Posts: n/a
 
      12-16-2005

vashw...@rediffmail.com schrieb:

> #include <stdio.h>
>
> int main()
> {
> FILE *fp;
> char s[100];
>
> fopen("file.txt","w+");
>
> fprintf(fp,"HI\n");
>
> fclose(fp);
> fscanf(fp,"%s",s);
> printf("%s\n",s);
> }
>
> I know this will invoke undefined behaviour.Does undefined behavior
> mean it MIGHT also work as expected in this case?


No, it means, it will actually work as expected in most cases. When
using an uninitialized pointer, I expect a segmentation fault. However,
the variable fp could by chance have the same value as stdin, so that
once in some million years it would read something from stdin. This,
however, would be totally unexpected behaviour.

 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      12-16-2005
wrote:

> #include <stdio.h>
>
> int main()
> {
> FILE *fp;
> char s[100];
>
> fopen("file.txt","w+");
>
> fprintf(fp,"HI\n");
>
> fclose(fp);
> fscanf(fp,"%s",s);
> printf("%s\n",s);
> }
>
> I know this will invoke undefined behaviour.Does undefined behavior
> mean it MIGHT also work as expected in this case?


Sure, if you want to risk your neck, feel free. If you want to risk it
working fine on your testing machine, but corrupting data on your users'
differently specced boxes, and your users are OK with that, go right
ahead. Make absolutely sure not to try such tricks on _my_ data, though.

Richard
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      12-16-2005
wrote:

> #include <stdio.h>


> int main()
> {
> FILE *fp;
> char s[100];
> fopen("file.txt","w+");


Until you replace this with what you presumably meant, namely

fp=fopen("file.txt","w+");

, the probability of this program working "as expected" is effectively
zero.

> fprintf(fp,"HI\n");
> fclose(fp);
> fscanf(fp,"%s",s);
> printf("%s\n",s);
> }


> I know this will invoke undefined behaviour.Does undefined behavior
> mean it MIGHT also work as expected in this case?Is it possible to read
> from a file which already closed at least once out of 1000 times on
> thousand different environment?


Sure, it MIGHT work. Why on earth would you want to take the chance?
Programmers don't deal in "maybe", and neither do customers.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      12-16-2005
wrote:
> #include <stdio.h>
>
> int main()
> {
> FILE *fp;
> char s[100];
>
> fopen("file.txt","w+");


This is useless. The result of the fopen() call needs to be assigned to
a FILE *. In your case
fp = fopen("file.txt", "w+");
>
> fprintf(fp,"HI\n");
>
> fclose(fp);
> fscanf(fp,"%s",s);


And if fp had been fopened once, it isn't anymore. Dead again.

> printf("%s\n",s);
> }
>
> I know this will invoke undefined behaviour.


Since you never connect fp to a stream, the three statements after the
fopen() all try to use an uninitialized pointer.

> Does undefined behavior
> mean it MIGHT also work as expected in this case?


Ain't no chance in hell your code will work, unless whatever way your
implementation handles fprintf(), fclose(), and fscanf() calls using an
uninitialized pointer-to-FILE is your expectation.

> Is it possible to read
> from a file which already closed at least once out of 1000 times on
> thousand different environment?


Just open the damn thing.


 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      12-16-2005
Ingo Menger wrote:
> vashw...@rediffmail.com schrieb:
>
>
>>#include <stdio.h>
>>
>>int main()
>>{
>> FILE *fp;
>> char s[100];
>>
>> fopen("file.txt","w+");
>>
>> fprintf(fp,"HI\n");
>>
>> fclose(fp);
>> fscanf(fp,"%s",s);
>> printf("%s\n",s);
>>}
>>
>>I know this will invoke undefined behaviour.Does undefined behavior
>>mean it MIGHT also work as expected in this case?

>
>
> No, it means, it will actually work as expected in most cases.


His code, of course, will *never* work. "Never work" and "will actually
work as expected in most cases" are not synonyms.

 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      12-16-2005
On 2005-12-16, <> wrote:
> #include <stdio.h>
>
> int main()
> {
> FILE *fp;
> char s[100];
>
> fopen("file.txt","w+");


I'll assume you meant fp = fopen(...);

> fprintf(fp,"HI\n");
>
> fclose(fp);
> fscanf(fp,"%s",s);


after fclose(), the value of its argument becomes indeterminate.

> printf("%s\n",s);
>}
>
> I know this will invoke undefined behaviour.Does undefined behavior
> mean it MIGHT also work as expected in this case?Is it possible to read
> from a file which already closed at least once out of 1000 times on
> thousand different environment?


undefined behavior can mean anything. on most systems, it probably
doesn't mean you get to read from the file. even if it doesn't free()
the underlying file pointer, it'll probably still close the file [say,
posix close()]
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-16-2005
writes:
> #include <stdio.h>
>
> int main()
> {
> FILE *fp;
> char s[100];
>
> fopen("file.txt","w+");
>
> fprintf(fp,"HI\n");
>
> fclose(fp);
> fscanf(fp,"%s",s);
> printf("%s\n",s);
> }
>
> I know this will invoke undefined behaviour.Does undefined behavior
> mean it MIGHT also work as expected in this case?Is it possible to read
> from a file which already closed at least once out of 1000 times on
> thousand different environment?


If your program invokes undefined behavior, it's always possible that
it will behave as you expect, whatever your expectations happen to be.
(That's not quite true; if you expect it to do something physically or
logically impossible, that's not going to happen -- but as far as the
standard is concerned, anything is possible.)

In this particular case, assuming you assign the result of fopen() to
fp, if the program displays "HI", it *probably* indicates that the
implementation is broken (e.g., fclose() doesn't work properly). If
the breakage only shows up for programs that invoke undefined
behavior, the implementation could still be conforming.

More realistically, consider something like this:

fp1 = fopen("some_file", "r");
...
fclose(fp1);
fp2 = fopen("another_file", "r");
fscanf(fp1, "%s", s);

Since fp1 is closed before fp2 is opened, it's plausible that fopen()
could return the same pointer value for both. In this case, reading
from fp1, even though it's closed, would most likely read from
"another_file". (I've just demonstrated this in a small program.)

As I'm sure you know, the lesson is: Don't do this.

--
Keith Thompson (The_Other_Keith) kst- <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.
 
Reply With Quote
 
Markus Moll
Guest
Posts: n/a
 
      12-16-2005
Hi

Keith Thompson wrote:

> If your program invokes undefined behavior, it's always possible that
> it will behave as you expect, whatever your expectations happen to be.
> (That's not quite true; if you expect it to do something physically or
> logically impossible, that's not going to happen -- but as far as the
> standard is concerned, anything is possible.)


Who says so?
I'll build a computer that sets fire to the programmer's house every time he
or she invokes undefined behavior!
On second thought, maybe not _every_ time. That would be too well
defined

SCNR
Markus

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dumb, Dumb Vista Au79 Computer Support 4 02-11-2007 03:40 PM
Probably a dumb s/// question. Mark Healey Perl 2 03-16-2005 04:51 PM
Dumb, dumb dumb Qestion David Napierkowski Digital Photography 6 10-31-2004 11:14 PM
dumb newbie question (or newbie dumb question) Jerry C. Perl Misc 8 11-23-2003 04:11 AM
Re: Dumb question Walter Roberson Cisco 1 07-23-2003 01:05 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57