Problem with very simple code

This is a discussion on Problem with very simple code within the C++ Programming forums, part of the General Programming Boards category; Please save yourself from developing bad habits early in your programming careers. Never use goto. Also, conforming to modern coding ...

  1. #16
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Please save yourself from developing bad habits early in your programming careers. Never use goto. Also, conforming to modern coding conventions will allow you to be accepted amoung the OOP community. People won't laugh at you and your code.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  2. #17
    Banned
    Join Date
    Oct 2004
    Posts
    250
    would using functions instead of goto slow down larger prorgams?

  3. #18
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Generally, when you are attempting to make your program faster, you use a profiler or some other tool to identify what parts of your code need to be optimized. In most cases, you need only change your algorithm, and not the programming structures you are using to implement that algorithm.

    It is possible, in some rare cases, that you might obtain a performance benefit from using goto over certain other options, however, you might as well use assembly if you really want the performance gain. If you are a C++ programmer, and you are worried about that type of overhead, then you should be using C. In most cases I would guess that the fear of the program being slower is unfounded and actually leads to more time being spent debugging and maintaining the goto than is saved by using it.

  4. #19
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Wouldn't void functions that take no parameters be a close if not exact equivalent of a goto? It's a jump in code execution, with no added stack allocation/deallocation for parameters that I can think of.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #20
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Quote Originally Posted by Hunter2
    Wouldn't void functions that take no parameters be a close if not exact equivalent of a goto? It's a jump in code execution, with no added stack allocation/deallocation for parameters that I can think of.
    no. functions translate to CALL and RETURN type of assembly instructions. No parameter makes it so that less stuff needs to be PUSHed and POPed, but still is much slower. Aside from that, goto is one direction and doesn't return.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #21
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Quote Originally Posted by cgod
    would using functions instead of goto slow down larger prorgams?
    yes, I think functions were a bad example. however lets use a "for instance". In the code example you presented you changed a while loop to a conditional goto. This gained nothing in the area of speed. The dissasembly should show pretty much the same output.

    For the most part, code that uses a goto can directly translate to an easier to read construct. There are some occasions (when you haven't put any thought into your algorithm) that there is no direct translation and it would take a little more effort to convert the function. Best idea is to just not use it as a rule. It gains you nothing and causes a world of pain.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #22
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    no. functions translate to CALL and RETURN type of assembly instructions. No parameter makes it so that less stuff needs to be PUSHed and POPed, but still is much slower.
    I stand corrected.

    Aside from that, goto is one direction and doesn't return.
    I was thinking of a block of code that you goto, then at the end of the block there's another goto that jumps back to where you were. On second thought though, I realize that that still wouldn't work.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using java programs within C code
    By lemania in forum Linux Programming
    Replies: 1
    Last Post: 05-08-2005, 02:02 AM
  2. problem with some simple code =/
    By Josh Norton in forum C++ Programming
    Replies: 3
    Last Post: 02-23-2004, 05:27 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Simple Compile Time Problem - HELP!
    By kamikazeecows in forum Windows Programming
    Replies: 2
    Last Post: 12-02-2001, 12:30 PM
  5. Big Code, Little Problem
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 10-03-2001, 05:14 PM

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