gotos

This is a discussion on gotos within the A Brief History of Cprogramming.com forums, part of the Community Boards category; I doubt that block of code would cause performance issues regardless if you use a goto or not....

  1. #16
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    I doubt that block of code would cause performance issues regardless if you use a goto or not.

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by psychopath View Post
    Code:
    void draw(){
        if(WeCantSeeTheObject()){
            goto skip;
        }
    
        /* rendering code */
    
    skip: /*do nothing*/ ;
    
    }
    I've found this to be faster than using if/else to skip or run the drawing code for some reason. It was a while ago, so I could've done something else wrong.
    There should be little difference betwen
    Code:
    if(!WeCantSeeTheObject()) {
        ... rendering code ... 
    }
    and the solution you posted. If there is, it's most likely because of the code generator in the compiler believes that the !WeCantSeeTheObject is not the same hit-rate as it really is (that is, the compiler is trying to optimize for a different rate of invisible objects than the code really has). But really, the code should be very close between those two cases.

    gcc has a "predictor likelyhood" extension that can tell the compiler that something is likely or unlikely to he true - that way, the compiler can string together the likely variations in a straight line, and the unlikely ones out of line.

    --
    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
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,627
    Code:
    int main()
    {
       mylab:;
       goto mylab;
       return 0;
    }
    Going backwards can easily be handled.
    Code:
    void goto_mylab() {
       goto mylab;   }
    
    int main()
    {
       mylab:;
       goto_mylab();
       return 0;
    }
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  4. #19
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    Except you can't use goto to jump from one function to another.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #20
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    When I used to use them before (I mean when I started), I used to amuse myself like so:

    Code:
    int main( void ) {
     /* blah */
    
      hell:;
    
      /* blah2 */
    
      goto hell; // <- twomers amused
    
      /* blah3 */
    
      return 0;
    }
    I used to have others that I can't remember now though.

  6. #21
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    What does this print, and why?
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main ( ) {
        int a = 4;
        switch ( a  ) {
            case 1: case 2:
                printf("1 or 2\n" );
                break;
            case 3:
                printf("3\n" );
                break;
            defualt:
                printf("Something else\n");
                break;
        }
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #22
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Default is misspelt ... so it'll err.

  8. #23
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    3\n
    Something else\n

    I do believe.

    EDIT: Wait... that's just stupid. Ignore that. It'll print nothing, right? ... I feel intensely retarded today.
    EDIT2: Well I've set MSVC to flag warnings as errors, unreferenced label.
    Last edited by cboard_member; 10-04-2007 at 01:18 PM.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  9. #24
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> 3\n
    >> Something else\n

    Am I missing something ... ?

  10. #25
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by twomers View Post
    Default is misspelt ... so it'll err.
    No, it won't. It will possibly warn that nothing is using defualt label, but it will compile. And it won't print anything since the default "default:" is an empty case.

    --
    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.

  11. #26
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by matsp View Post
    No, it won't. It will possibly warn that nothing is using defualt label, but it will compile. And it won't print anything since the default "default:" is an empty case.

    --
    Mats
    Yup, something like that:
    Code:
    warning C4102: 'defualt' : unreferenced label
    warning C4702: unreachable code
    0 error(s), 2 warning(s)
    There is no need for goto's. They're for people who haven't yet fully embraced structured programming concepts.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #27
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    That's a big bump :\

    Nothing a goto couldn't fix
    Code:
    /* ... */
        
        defualt:
            printf("Something else\n");
        break;
        
        default:
            goto defualt;
        break;
    }
    /* ... */

  13. #28
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Please don't bump old threads

  14. #29
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Whats so bad about bumping old threads? As long as you arent harassing other people for attention that is.

  15. #30
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Doesn't really matter why it's bad, but it does matter that it is bad
    Quote Originally Posted by Rule #5
    5. Don't bump threads. (Bumping: posting messages on threads to move them up the list or to post on a thread that has been inactive for two weeks or longer).
    Naughty iMalc!

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GoTo's?
    By Neo1 in forum C++ Programming
    Replies: 4
    Last Post: 07-09-2007, 03:24 AM
  2. Time limit on cin
    By Queatrix in forum C++ Programming
    Replies: 11
    Last Post: 04-13-2005, 01:56 PM
  3. C++ vs Java
    By Moni in forum C++ Programming
    Replies: 19
    Last Post: 03-23-2003, 03:19 PM
  4. Hehe, look at this code. (Not looking for help)
    By SinAmerica in forum C++ Programming
    Replies: 22
    Last Post: 05-04-2002, 09:30 PM
  5. if, then, and gotos
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 04-03-2002, 04:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21