C/C++

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • A biiiig break ?

    7 answers - 458 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    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,
  • No.1 | | 456 bytes | |

    <@nowhere.wdwrites:
    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.
  • No.2 | | 90 bytes | |

    Thanks!! That sounds good. I'll experiment and come back if
    Cheers,
  • No.3 | | 1597 bytes | |


    "" <@nowhere.wdwrote
    [setjmp and longjmp]
    Thanks!! That sounds good. I'll experiment and come back if
    Cheers,

    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.

  • No.4 | | 839 bytes | |

    Thanks.

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

    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,

  • No.5 | | 945 bytes | |

    In article <42bf9064$0$899$8fcfb975@news.wanadoo.fr>
    <@nowhere.wdwrote:
    >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.)
  • No.6 | | 652 bytes | |

    Chris Torek wrote:
    In article <42bf9064$0$899$8fcfb975@news.wanadoo.fr>
    <@nowhere.wdwrote:
    >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

  • No.7 | | 461 bytes | |

    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 !

Re: A biiiig break ?


max 4000 letters.
Your nickname that display:
In order to stop the spam: 6 + 5 =
QUESTION ON "C/C++"

EMSDN.COM