473,405 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

A biiiig break ?

Dear all,

I would like to find an instruction that
stops n level of my programm ...
Typically, I have a gtk-graphical application (a game),
the user asks for something (say function do_that)
and something wrong happens
so I would like the program to abort the current computation
(end of do_that)and go back to its usual "waiting" state

Any inkling on how such a behaviour should be coded ?
Best,
Amities,
Olivier
Nov 15 '05 #1
7 1088
Olivier <Ol**@nowhere.wd> writes:
I would like to find an instruction that
stops n level of my programm ...
Typically, I have a gtk-graphical application (a game),
the user asks for something (say function do_that)
and something wrong happens
so I would like the program to abort the current computation
(end of do_that)and go back to its usual "waiting" state


Sounds like a job for setjmp() and longjmp(). See your C textbook for
details.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Nov 15 '05 #2
Thanks!! That sounds good. I'll experiment and come back if ...
Cheers, O.
Nov 15 '05 #3

"Olivier" <Ol**@nowhere.wd> wrote
[setjmp and longjmp]
Thanks!! That sounds good. I'll experiment and come back if ...
Cheers, O.

Keith Thompson's advice isn't wrong as such, but it is fairly unusual
to see setjmp(0 and longjmp() in C code. It breaks the normal flow of the
language.

C++ exception handling uses a similar mechanism, but is much better because
items on the stack are properly destroyed. You might consider going to C++.
C isn't the best language for absolutely every problem.

Another way is to make all your function return an "abort" code. The
function that detects the error aborts and returns -1, the caller detects it
and returns -1 itself, and so on until control goes back where it wants to
be.

Another techniques is to use "sticky errors". Let's say the function is to
draw some complex graphics on an image. In your image structure, put an
"error" flag, set wheneve someone tries to draw a pixel out of bounds,
assuming this is your error.

Drawing can physically continue, but if the previous operation was an error
then the subsequent drawing is invalid (if you can't draw the yellow circle
correctly, then putting on the two dots and smile isn't going to work). Low
level functions don't need to worry about this. When the high level function
examines the image, it sees that the error flag is set. The yellow circle
wasn't drawn, and the eyes and mouth have probably messed up somethig else.
So it simply discards the whole image.

Nov 15 '05 #4
Thanks.

What I'm really looking for is the emacs lisp way
of handling errors :
(catch 'this-flag
function
function-apply-if-this-flag-has-been-caught)

and function reads
(defun function
....
if(error) {throw 'this-flag};
)

When the throw command is issued, the catch
process is applied. That way errors are clearly
identified.

Maybe I'll do that with gotos finally, since setjmp
is too strong: something have happened that I don't
erased! Well, for instance the last message to the
user telling him/her what is happening :-)

catch-throw give a well-defined surrounding for
these gotos, in a somewhat encapsulated world.
Well, well, emacs-lisp is programmed in C if I
remember well, maybe I should go and look at the
way it is coded there :-)

Cheers,
Olivier
Nov 15 '05 #5
In article <42*********************@news.wanadoo.fr>
Olivier <Ol**@nowhere.wd> wrote:
What I'm really looking for is the emacs lisp [catch and throw] ...


To implement catch, throw, and unwind-protect, there is a great deal
of machinery embedded in a Lisp system. C lacks this machinery.

You can attempt to simulate it with setjmp and longjmp, but this
requires a lot of care. A "call" to the longjmp() macro is the
"goto" they tell you to avoid in beginning programming classes. :-)
Worse, "longjmp" invalidates the values of many variables, requiring
you to sprinkle your code liberally with "volatile" modifiers.

C++ also has catch and throw (and most of the machinery), but lacks
an explicit unwind-protect. (You can fake the last by using an
automatic variable instance of a class object that has a destructor,
but you lose some control over order-of-execution.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 15 '05 #6
Chris Torek wrote:
In article <42*********************@news.wanadoo.fr>
Olivier <Ol**@nowhere.wd> wrote:
What I'm really looking for is the emacs lisp [catch and throw] ...


To implement catch, throw, and unwind-protect, there is a great deal
of machinery embedded in a Lisp system. C lacks this machinery.

You can attempt to simulate it with setjmp and longjmp, but this
requires a lot of care. A "call" to the longjmp() macro is the
"goto" they tell you to avoid in beginning programming classes. :-)


setjmp is a macro, longjmp is a function.

Robert Gamble

Nov 15 '05 #7
> To implement catch, throw, and unwind-protect, there is a great deal
of machinery embedded in a Lisp system. C lacks this machinery.


Aie. I'm surprised at that, but I'm so used to the flexibility
of lips :-( Except that for graphical stuff, or linear programming,
well I can't quite avoid C: it is too used nowadays :-)

So I'll implement a by-hand error control with states and so on :-(
Thanks !
O.
Nov 15 '05 #8

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

Similar topics

5
by: Martin Lucas-Smith | last post by:
Is there any need to keep the final break in a switch which uses a default at the end? I.e: switch ($data) { case 'foo': # Action break;
5
by: Ann | last post by:
I have trouble sometimes figuring out where break and continue go to. Is there some easy way to figure it out, or a tool? TIA Ann
1
by: Scanner2001 | last post by:
I am trying to insert a printer page break inside of a form. I do not seem to be able to do this. My form contains 3 asp panels. Each of the panels have dynamically built content, and they are...
5
by: viza | last post by:
Hi! Suppose I have int i,j,k; for(i=0;i<I;++i){ /* loop 1 */ for(j=0;j<J;++j){ /* loop 2 */ for(k=0;k<K;++k){ /* loop 3 */ if(test){
25
by: chunhui_true | last post by:
In <<expert c>>I know the break in if wich is scoped in switch is break the switch,like: switch c case 1: if(b){ break; } ...... But like this: while(a){
5
by: tony collier | last post by:
To break out of a loop i have seen some people use RETURN instead of BREAK I have only seen RETURN used in functions. Does anyone know why RETURN is used instead of BREAK to kill loops?
2
by: yyhhjj | last post by:
I created a test program to implement an iterator. First, I used 'yield break' in the iterator, it worked normally. Then, I simply used 'break' in the places of 'yield break', it still worked...
3
by: Barbara Alderton | last post by:
I recently wrote an ASP.NET app that used user controls. I produced a printer-friendly output form. One requirement was a pagebreak between one part of the form and another. I had no problem...
26
by: Alexander Korsunsky | last post by:
Hi! I have some code that looks similar to this: -------------------------------------------- char array = "abcdefghij"; for (int i = 0; i < 10; i++) {
7
by: =?Utf-8?B?aXdlYg==?= | last post by:
Can we set page breaks in ASP Pages dynamically based on the data being populated? Akshay.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
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...

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.