Red Hat axp-list: Linux on Alpha (1995-2022)
 help / color / mirror / Atom feed
* Re: fclose() (fwd)
@ 1997-02-22  1:02 Eric Smith
  0 siblings, 0 replies; 8+ messages in thread
From: Eric Smith @ 1997-02-22  1:02 UTC (permalink / raw)


"Bryan W. Headley" <bheadley@interaccess.com> wrote:
> That's incredibly stupid. Is this a /dev/null trick ("read all you want,
> there's plenty of nothing for you"). Let's say I open /etc/passwd three times
> and start reading records from each (but not necessarily synchronized; f1
> reads 20 bytes, f2 reads 30 bytes, f3 lseeks to the end of file - 50 bytes)
> There's no way these three pointers can be to the same address;

I think you missed the point of that test program.  It opened and closed
/dev/null three times sequentially; there were never two streams open to
it at the same time.

The point was that once you fclose() a stream, a later fopen() may return
the same file pointer.  There are at least two possible reasons for this.  The
FILE structures may be in a static array, and get reused, or they may be
dynamically allocated, and it may just happen that the same space gets
malloc'd again.

This demonstrates why you should *never* fclose() a file twice.  The original
poster complained that fclose() should check whether the pointer is a valid
file pointer, but that won't solve the problem if the same file pointer gets
reused.

> there's no excuse for fclose()ing one automagically closing another

It doesn't.  It's only when you do things with files after closing them
that unpredictable things happen.  ANSI (and presumably ISO) document that.

> (esp. if you fclose, it twice, because, well, you're a fool, but you have
> a license to be a fool on the wall)

If you have a license to be a fool, you automatically get with it (at no
extra charge) a license to have buggy code.

If you can't keep track of whether you've closed a file or not, you should
test the file pointer for NULL before attempting to close it, and set it
to NULL after closing it.

Cheers,
Eric

--
To unsubscribe: send e-mail to axp-list-request@redhat.com with
'unsubscribe' as the subject.  Do not send it to axp-list@redhat.com

^ permalink raw reply	[flat|nested] 8+ messages in thread
[parent not found: <9702181221.AA02768@halfpint.rtp.avanticorp.com>]
[parent not found: <9702171536.ZM1175@davros.bwh.com>]
* Re: fclose() (fwd)
@ 1997-02-17 22:55 Stephen Williams
  0 siblings, 0 replies; 8+ messages in thread
From: Stephen Williams @ 1997-02-17 22:55 UTC (permalink / raw)


> FILE *f1, *f2;
>
> f1 = fopen("file1", "r");
> fclose(f1);
> f2 = fopen("file2", "r");
> fclose(f1); /* Second close of f1 */

bheadley@interaccess.com said:
> Yeech! I wonder what it does with three potential file *'s. That 
> downright sucks; it's nice that they keep their FILE *'s in a stack 

FILE structures are not necessarily mallocated. ...

The C standard allows (expects?) an implementation to limit the number
of open FILEs, and static arrays are not an unreasonable implementation
in some cases. (Hell, I even considered it myself once.)

-- 
Steve Williams
steve@icarus.com
steve@picturel.com

"The woods are lovely, dark and deep.  But I have promises to keep,
And lines to code before I sleep, And lines to code before I sleep."

--
To unsubscribe: send e-mail to axp-list-request@redhat.com with
'unsubscribe' as the subject.  Do not send it to axp-list@redhat.com

^ permalink raw reply	[flat|nested] 8+ messages in thread
[parent not found: <jlnance@avanticorp.com>]
* Re: fclose() (fwd)
@ 1997-02-17 21:18 Jim Nance
  0 siblings, 0 replies; 8+ messages in thread
From: Jim Nance @ 1997-02-17 21:18 UTC (permalink / raw)


Forwarded message:

> > In professional world, I've seen far too many programs that close all
> > handles at termination, regardless of whether if known that handle is
> > open or not. It WOULD be nice to legislate good programming, I agree...

I wonder why they bother.  They all get closed when the program terminates.
But thats not what I am writing about.  A little gem of experience I gained
under SunOS is that the code sequence:

FILE *f1, *f2;

f1 = fopen("file1", "r");
fclose(f1);
f2 = fopen("file2", "r");
fclose(f1); /* Second close of f1 */

will close f2 when you call fclose on f1 the second time because the second
fopen() returns the same pointer that the first fopen() returned.  Ie the
file pointers get recycled.  This can be most unpleasant.  I guess my point
is that even if we make fclose() not crash, there is no guarantee that its
going to run like the programmer intended.

Will Electric Fence catch these violations?  I know that Purify does and it
is a nice feature.

Jim

--
To unsubscribe: send e-mail to axp-list-request@redhat.com with
'unsubscribe' as the subject.  Do not send it to axp-list@redhat.com

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~1997-08-18 16:21 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <gaf@zk3.dec.com>
1997-02-21  4:59 ` fclose() (fwd) Bryan W. Headley
1997-08-18 16:21 ` Question on using manual monitor/kbd switch Dave Gavin
1997-02-22  1:02 fclose() (fwd) Eric Smith
     [not found] <9702181221.AA02768@halfpint.rtp.avanticorp.com>
1997-02-18 16:14 ` Jerry Feldman
     [not found] <9702171536.ZM1175@davros.bwh.com>
1997-02-18 12:21 ` Jim Nance
  -- strict thread matches above, loose matches on Subject: below --
1997-02-17 22:55 Stephen Williams
     [not found] <jlnance@avanticorp.com>
1997-02-17 21:36 ` Bryan W. Headley
1997-02-17 21:18 Jim Nance

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox