473,473 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2266
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: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.