goto question

This is a discussion on goto question within the C++ Programming forums, part of the General Programming Boards category; today i was looking at a program online and i noticed the command goto, of which i was unfamiliar with. ...

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

    goto question

    today i was looking at a program online and i noticed the command goto, of which i was unfamiliar with. i now realize what it does and how it works, but when i asked my teacher why he hadn't taught us about it he said he learned back when he was in school that it was bad programming. he said he wasn't sure if this was true so i was wondering what are (if any) of the downsides of using this command.

    thanks in advance
    "uh uh uh, you didn't say the magic word"
    -Jurassic Park

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    it is bad practice to jump around a subroutine with no rhyme or reason. This is what goto allows. There is a matter of program flow. Program flow can be beautiful if it goes through the basic loops and conditional statements and ends at the end of the function EVERY TIME. goto bypasses that system. It creates confusing code. While it is most definitely still valid, it should be avoided.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

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

    lol fair enough

    but assuming im careful it's ok... like no risk of memory leaks etc?
    "uh uh uh, you didn't say the magic word"
    -Jurassic Park

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    yes, if you are careful it is ok. It translates to a JMP assembler instruction which is of course what most of the other stuff eventually turns into anyway. However, most people would tell you not to use it anyway.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    161
    > like no risk of memory leaks etc?

    That depends on your use. Consider this code:
    Code:
      void somefunc()
      {
          MyClass* p = new MyClass;
          goto end;
          delete p;
        end:
          return;
      }
    That code is obviously oversimplistic, and could be avoided by using a std::auto_ptr, but the main problem with goto is that you lose the predictability of your code.

    However, I would never suggest to ban it's use outright. If you're writing the inner loop for an interpreter and you find that a goto can give you a 5-15% increase in speed (check out Ximian's Mono implementation of .Net), then there is no reason not to use it.

    That said, for most situations, there is a more elegant solution than sticking indiscriminant jmps in your code.

    -tf

  6. #6
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Many programmers wouldn't touch a project with a goto statement.

    There are well-structured programs, and then there's goto.

  7. #7
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    I recently participated in a regional programming competition sponsored by the ACM. The goal was to solve problems as quickly as possible, so that meant a lot of dirty coding rather that what is normally taught to programmers. I was the only on my team who knew had to properly use goto. Although we could've done without goto, doing so would've been much more difficult.

    I'd never be the first to suggest using goto in a business or academic setting, though.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  8. #8
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    A truly good optimizing compiler should be able to negate any benefit you derive from goto.

    I don't use it, ever, and it's not considered good practice. You can easily abuse it.
    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.

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    avoiding goto was one of the bigger accomplishments of languages like C++... your programming teacher should be familiar with the term 'spaghetti code'
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Although I never use it, I'm glad it's part of the language.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  11. #11
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I book marked this page for when this debate next came up.
    http://www.azillionmonkeys.com/qed/goto.html

    I use it all the time in C to roughly emulate structured exception handling and you will see it fairly often around the place in this use.

  12. #12
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,630
    http://www.azillionmonkeys.com/qed/goto.html
    I'm gonna make this my signature!!!

    gg

  13. #13
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,630
    Did it work?

    gg

    [EDIT]
    Woo hoo!

  14. #14
    A Banana Yoshi's Avatar
    Join Date
    Oct 2001
    Posts
    857
    spaghetti code mon!
    Yoshi

  15. #15
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Originally posted by anonytmouse
    I book marked this page for when this debate next came up.
    http://www.azillionmonkeys.com/qed/goto.html

    I use it all the time in C to roughly emulate structured exception handling and you will see it fairly often around the place in this use.
    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.

    I tend to disagree with that author, as well, as I have yet to see a real-world set of code that looks better when written with gotos vs. other forms of flow control. I don't disagree with the sentiment that you should use the best written code at all times, I just don't think gotos improve readability.
    Last edited by Cat; 11-19-2003 at 01:23 AM.
    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.

Page 1 of 2 12 LastLast
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, 08: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, 04:05 PM

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