Red Hat axp-list: Linux on Alpha (1995-2022)
 help / color / mirror / Atom feed
* Re: fclose() (fwd)
@ 1997-02-17 21:18 Jim Nance
  0 siblings, 0 replies; 7+ 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] 7+ messages in thread

* Re: fclose() (fwd)
       [not found] <jlnance@avanticorp.com>
@ 1997-02-17 21:36 ` Bryan W. Headley
  0 siblings, 0 replies; 7+ messages in thread
From: Bryan W. Headley @ 1997-02-17 21:36 UTC (permalink / raw)


On Feb 17,  4:18pm, Jim Nance wrote:
> Subject: Re: fclose() (fwd)
> 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.
>
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

-- 
Bryan W. Headley                    Home:   bheadley@interaccess.com
211 East Pineview Drive             Office: bwh@abnamro.com
Round Lake Park, IL 60073           Phone:  847 740-1446

--
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] 7+ messages in thread

* Re: fclose() (fwd)
@ 1997-02-17 22:55 Stephen Williams
  0 siblings, 0 replies; 7+ 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] 7+ messages in thread

* Re: fclose() (fwd)
       [not found] <9702171536.ZM1175@davros.bwh.com>
@ 1997-02-18 12:21 ` Jim Nance
  0 siblings, 0 replies; 7+ messages in thread
From: Jim Nance @ 1997-02-18 12:21 UTC (permalink / raw)


> 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

I just checked and Linux and Digital Unix both do the same thing.  Heres
the program:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int i;
    for(i=0; i<3; i++) {
        FILE *f = fopen("/dev/null","r");
        printf("f = %x\n", f);
        fclose(f);
    }
    return 0;
}

and here is the Linux output:

jlnance-pc> a.out
f = 8049650
f = 8049650
f = 8049650

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] 7+ messages in thread

* Re: fclose() (fwd)
       [not found] <9702181221.AA02768@halfpint.rtp.avanticorp.com>
@ 1997-02-18 16:14 ` Jerry Feldman
  0 siblings, 0 replies; 7+ messages in thread
From: Jerry Feldman @ 1997-02-18 16:14 UTC (permalink / raw)


> I just checked and Linux and Digital Unix both do the same thing.  Heres
> the program:
Not quite. On Digital Unix, I change %x to %lx. Pointers are 64 bits. 
> #include <stdio.h>
> #include <stdlib.h>
> 
> int main(void)
> {
>     int i;
>     for(i=0; i<3; i++) {
>         FILE *f = fopen("/dev/null","r");
>         printf("f = %lx\n", f);
>         fclose(f);
>     }
>     return 0;
> }
> 
> and here is the Digital Unix output:
sauron:assem [1528%] a.out
f = 3ffc0080348
f = 3ffc0080380
f = 3ffc0080380

-- 
+--------------------------------------------------------------------+
Jerry Feldman            Digital Unix Development Environment
Digital Equipment Corp.  (work)gaf@zk3.dec.com (home)gaf@highway1.com
110 Spitbrook Rd.        Mailstop: ZKO3-2/W17
Nashua, NH 03062-9987    (603)881-2970, DTN:381-2970
+--------------------------------------------------------------------+
 

--
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] 7+ messages in thread

* Re: fclose() (fwd)
       [not found] <gaf@zk3.dec.com>
@ 1997-02-21  4:59 ` Bryan W. Headley
  0 siblings, 0 replies; 7+ messages in thread
From: Bryan W. Headley @ 1997-02-21  4:59 UTC (permalink / raw)


On Feb 18, 11:14am, Jerry Feldman wrote:
> Subject: Re: fclose() (fwd)
>
> > I just checked and Linux and Digital Unix both do the same thing.  Heres
> > the program:
> Not quite. On Digital Unix, I change %x to %lx. Pointers are 64 bits.
> > #include <stdio.h>
> > #include <stdlib.h>
> >
> > int main(void)
> > {
> >     int i;
> >     for(i=0; i<3; i++) {
> >         FILE *f = fopen("/dev/null","r");
> >         printf("f = %lx\n", f);
> >         fclose(f);
> >     }
> >     return 0;
> > }
> >
> > and here is the Digital Unix output:
> sauron:assem [1528%] a.out
> f = 3ffc0080348
> f = 3ffc0080380
> f = 3ffc0080380
> --

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; there's no excuse for
fclose()ing
one automagically closing another (esp. if you fclose it twice, because, well,
you're a fool, but you have a license to be a fool on the wall)

-- 
Bryan W. Headley                    Home:   bheadley@interaccess.com
211 East Pineview Drive             Office: bwh@abnamro.com
Round Lake Park, IL 60073           Phone:  847 740-1446

--
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] 7+ messages in thread

* Re: fclose() (fwd)
@ 1997-02-22  1:02 Eric Smith
  0 siblings, 0 replies; 7+ 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] 7+ messages in thread

end of thread, other threads:[~1997-02-22  1:02 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1997-02-17 22:55 fclose() (fwd) Stephen Williams
  -- strict thread matches above, loose matches on Subject: below --
1997-02-22  1:02 Eric Smith
     [not found] <gaf@zk3.dec.com>
1997-02-21  4:59 ` Bryan W. Headley
     [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
     [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