472,993 Members | 2,022 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,993 software developers and data experts.

How dirty is setjmp+fopen+longjmp ?

Hi,

wondering about:

func1: setjmp() ; func2();
func2: {FILE *f; f = fopen(); func3(); fclose(f)}
func3 : if() longjmp; else return;

Note that FILE *fis a local variable in func2.

How dirty is that ?
Will some dirty things remain in the RAM ?
file is in RAMDISK and is intended to be opened thousands of time;
don't ask me why.
Is there a risk to have fopen() fail because of too many times opened
without having been closed ?

Or in contrary, is it clean? After all, FILE *f is local, fopen is
after the setjmp...

System is unix in case it has some importance.

--
« nous devons agir comme si la chose qui peut-être ne sera pas devait
être » (Kant, Métaphysique des moeurs, doctrine du droit, II conclusion)

Thomas Baruchel <ba******@laposte.net>
Nov 13 '05 #1
2 2240
In article <3f***********************@news.free.fr>,
Thomas Baruchel <th*************@libertysurf.fr> wrote:
Hi,

wondering about:

func1: setjmp() ; func2();
func2: {FILE *f; f = fopen(); func3(); fclose(f)}
func3 : if() longjmp; else return;

Note that FILE *fis a local variable in func2.

How dirty is that ?
Will some dirty things remain in the RAM ?
file is in RAMDISK and is intended to be opened thousands of time;
don't ask me why.
Is there a risk to have fopen() fail because of too many times opened
without having been closed ?
If the longjmp gets called, the file you opened in func2 won't get
closed. Typically this means that anything you wrote to the file
(through the stdio FILE) may not have made it into the actual file
in the OS's filesystem; there's also usually a limit on the number of
"file descriptors" a program can have open, and opening the same file
multiple times will use multiple file descriptors.
Or in contrary, is it clean? After all, FILE *f is local, fopen is
after the setjmp...
Local variables after a setjmp disappear without any cleanup. The
buffers, OS file descriptors, and other fun things that the FILE * points
at will leak, because the call to fclose gets skipped by the longjmp.
There are a few ways of making this work (others may come up with
even more):

-Remove the setjmp/longjmp
Usually (but not always) there's a better (where "better" includes
factors like "cleaner", "easier", and "leaves the code more maintainable",
balanced appropriately) way to do things than to use setjmp and longjmp.
You haven't provided enough information for me to comment on whether
your case is likely to be an exception to this.

-Move the fopen/fclose outside the setjmp/longjmp
If this is a temporary file that the program uses quite a bit, it may
be better to do something like:
--------
func1:
open temporary file
call setjmp
do stuff including calling func2 (possibly multiple times)
close temporary file

func2:
do stuff with FILE * that comes from func1, including calling
func3

func3:
do stuff, possibly including calling longjmp to go back to the
setjmp in func1
--------
rather than opening and closing it in func2

-Make the FILE * static and make sure anything from a previous call
is cleaned up or re-used when func2 starts. Variables with static
duration are required to act the way you'd expect variables to act even
in the presence of setjmp and longjmp, so you can rewrite func2 to look
something like this:
--------
return_type func2(args)
{
static FILE *f=NULL;
if(f)
{
/*We've left the file open from a previous call (either
by longjmping out, or by deciding not to close it
when we're done. We may want to check or reset
some status, like where in the file we're looking,
or maybe just close and re-open it.
*/
rewind(f);
}
else
{
/*We don't currently have the file open, so open it*/
f=fopen("filename",mode_flags);
}

/*Do stuff here, including calling func3 (which may longjmp back
to func1)
*/

if(we_want_to_close_the_file)
{
fclose(f);
/*Don't forget to clear the local pointer as well, so that
we don't think the file is still open on the next call.
*/
f=NULL;
}

return return_value;
}
--------

System is unix in case it has some importance.


This affects exactly what the consequences of leaving a file unclosed
are, but won't affect the problem with longjmp not cleaning them up.
In general, unix-specific questions (which this one isn't) should go
to comp.unix.programmer.
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
I disagree. The best indicator of comp.lang.c activities is an
industrial-strength thermometer.
--Richard Heathfield in comp.lang.c
Nov 13 '05 #2
Thomas Baruchel wrote:

Hi,

wondering about:

func1: setjmp() ; func2();
func2: {FILE *f; f = fopen(); func3(); fclose(f)}
func3 : if() longjmp; else return;

Note that FILE *fis a local variable in func2.

How dirty is that ?
Filthy. Covered with mud and muck, not to mention
slime. Unhealthy, disease-ridden, and disgusting.
Will some dirty things remain in the RAM ?
file is in RAMDISK and is intended to be opened thousands of time;
don't ask me why.
Is there a risk to have fopen() fail because of too many times opened
without having been closed ?
Yes. If func3() jumps right back to func1(), func2()
never gets a chance to call fclose(). The implementation
is not required to support an infinite number of un-closed
streams.
Or in contrary, is it clean? After all, FILE *f is local, fopen is
after the setjmp...
This has nothing to do with anything, as far as I can see.
System is unix in case it has some importance.


It doesn't. If it did, the question would be off-topic.

--
Er*********@sun.com
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

40
by: Fao, Sean | last post by:
A guy at http://www.embedded.com gave his opinion on the C language and I was shocked to see the number of supporters for the author.  I also find it hard to believe that 99% of all code has memory...
22
by: Nimmi Srivastav | last post by:
Can someone kindly clarify the distinction between long jumps and gotos? Why is one frowned upon and not the other? Is there really any situation where use of longjmp becomes inevitable? A...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
4
by: Sreekanth | last post by:
Hi all, I have implemented a timing out version of fgets function call. I am pasting the entire code below. I have following doubts: 1. The code which I have written does it follow standard C...
66
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if...
72
by: jacob navia | last post by:
We have discussed often the proposition from Microsoft for a safer C library. A rationale document is published here by one of the members of the design team at microsoft:...
31
by: Francine.Neary | last post by:
One interesting thing to come out of the recent "Alignment" thread is that it is impossible to write a portable replacement for malloc in "user space" (not sure what the right term is - I mean an...
158
by: jacob navia | last post by:
1: It is not possible to check EVERY malloc result within complex software. 2: The reasonable solution (use a garbage collector) is not possible for whatever reasons. 3: A solution like the...
39
by: mathieu | last post by:
Hi there, I am trying to reuse a piece of code that was designed as an application. The code is covered with 'exit' calls. I would like to reuse it as a library. For that I renamed the 'main'...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.