Thread: goto question

  1. #16
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Why not just use setjmp/longjmp? This seems like it has all the advantages of the goto, plus the fact the target is not fixed at compile-time, but can be adjusted by the program as it needs.
    To me, it seems not much of an advantage not knowing the jump location at compile time. Intel processors don't predict indirect jumps, do they? (don't remember well...)
    I don't really see in what cases it is interesting to have the target modified.

  2. #17
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    One example is when you call longjmp in a library and you require the caller to have called setjmp previously. This is used in place of exceptions in a lot of C libraries, like libpng for one.

    It also means you could programmatically choose where your code goes on error, so you can make decisions at runtime about where you will handle errors; this may make it very easy to enable/disable a debug mode in your program.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #18
    Registered User heat511's Avatar
    Join Date
    Dec 2001
    Posts
    169

    wow

    didn't know i would spark such a huge response...

    altho i really don't see how it helps readability (doesn't for me anyway) i can see how using it to skip a huge section of if statements (for example) could really speed up the program, and this is something i desperately need in my opengl apps
    "uh uh uh, you didn't say the magic word"
    -Jurassic Park

  4. #19
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    why not do this instead? is it really that useful to use goto in this situation?
    Code:
    if(!condition)
    {
       if(something)
          //do something
       if(something)
          //do something
       if(something)
          //do something
       if(something)
          //do something
       if(something)
          //do something
       if(something)
          //do something
       if(something)
          //do something
    }
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #20
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    goto does not speed up anything as far as I know if if-statements are used correctly, except some special cases maybe.

  6. #21
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    ...Although we could've done without goto, doing so would've been much more difficult...
    Which is why I never use it. Most of the time I see it being used 'lazily' to solve a problem that can be done with better code.

    Goto is nasty. Those of you who know asm somewhat will already realize that there are about a trillion short jumps and maybe even hundreds of long jumps in your code already. But the whole issue comes down to the fact that C provides other more polished mechanism by which to accomplish what goto does. Goto is hard to trace, hard to debug, ugly, and violates one entry one exit. Jumping to a label in C is assinine and for those of you who blast me for assembly - you are basically adding jumps to the assembly that need not be there.

    Try to put the following in asm:

    Code:
    switch (x)
    {
      case 1:  DoOne();break;
      case 2:  DoTwo();break;
      case 3:  goto Assinine
    }
    
    goto SkipAssinine:
    
    Assinine:
      DoSomethingElse();
    
    SkipAssinine:
      ContinueWithProgram();
    See how the goto requires you to almost use assembly language fall-through in C? Very ugly. One use of goto often requires yet another uglier use of the same.

    Code:
      mov   eax,[x]
      cmp   eax,1
      jne    Compare2
      call    DoOne
      jmp   EndOfCompare                 ;only here if break is used
    Compare2:
      cmp   eax,2
      jne    Compare3
      call    DoTwo
      jmp   EndOfCompare                 ;only if break is used
    Compare3:
      cmp   eax,3
      jne    EndofCompare
      jmp   Assinine        
    
    Assinine:
       call DoSomethingElse
       ;Fall through to EndOfCompare
    
    EndOfCompare:
       call ContinueWithProgram

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick noob question
    By thanatos1 in forum C# Programming
    Replies: 2
    Last Post: 06-17-2009, 08:28 PM
  2. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. Need some help with a basic tic tac toe game
    By darkshadow in forum C Programming
    Replies: 1
    Last Post: 05-12-2002, 04:21 PM
  5. I need a faster way to do this....
    By frenchfry164 in forum C++ Programming
    Replies: 4
    Last Post: 01-28-2002, 05:05 PM