Thread: Any way to return to the top of main from a function

  1. #16
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by dwks View Post
    Another alternative that hasn't been mentioned is to use longjmp(), a sort of cross-function goto -- but there's a reason it hasn't been mentioned, and that is because this is not a good function to use at all!
    Yes -- using longjmp() would require you to first clean up all allocated resources (otherwise you'd leak a bunch of stuff on every "restart"). The logic involved in doing this is going to be basically the same logic as returning normally back up the call stack in the first place.

    Another silly method would be to re-exec() yourself. Actually, depending on the context (which is still completely unclear), that might not be such a bad option.

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'd still like to hear what the original poster would want to do - and yes, I had thought of longjmp, but as you mention, it's bad in the case of allocated memory.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    Registered User
    Join Date
    Apr 2008
    Posts
    19
    Sorry for my absence. I have had a lot of other things going on besides programming.

    For some reason I thought that converting my main function to a separate function and calling a recursion when necessary would be too much work and require too much hacking at my original code. After thinking about it more I don't see why this would be the case anymore. It should be quite simple actually. I will give that a try and get back to you guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Replies: 4
    Last Post: 11-23-2003, 07:15 AM