Thread: Not important, only a curiosity...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    106

    Not important, only a curiosity...

    Hello, I'm wondering if the use of goto is so wrong as some people say: I understand that this is to avoid the "spaghetti" problem ( I remember some "outstanding" C64 program... ) but used with wishdom I think it's a good possibility.

    For example, I can use it to jump directly to a common exit part of a function from any place in the function where I catch an error...

    BrownB

    Oh yes, another curiosity: how does the "rating" work in these forums?
    Last edited by BrownB; 12-21-2004 at 08:51 AM.

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    search the boards for both of those answers then ask again
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    </life>
    Join Date
    Oct 2004
    Posts
    83
    Structure your program correctly and there is no need for a goto statement.
    Microsoft is merely an illusion, albeit a very persistant one.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    goto is fine if used carefully. A good time for goto is common error handling code or leaving a deeply nested loop. Programs with need for performance can use goto too. While goto can be replaced with better control structures and performance is okay without it and goto does not make the code simpler, it should be avoided.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    The sample framework that came with the old DirectX SDK used to have a goto statement inside of the EnumerateDevices function. I've been looking at the updated code and it seems that the goto has since been removed.

  6. #6
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    I hear people get fired for using goto on a team effort project. lol.

  7. #7
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    I have seen a single useful goto in about 6 years of everyday professional coding. No, it's not totally useless, only in 99.99% of the cases. As a rule of thumb, if you see one, it's wrong.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Let's say you find a situation in which using goto is the better option. There would be more problems to using the goto and being criticized for it, than for using the alternatives and having a less effective solution.

  9. #9
    Quote Originally Posted by BrownB
    Hello, I'm wondering if the use of goto is so wrong as some people say: I understand that this is to avoid the "spaghetti" problem ( I remember some "outstanding" C64 program... ) but used with wishdom I think it's a good possibility.
    » The goto statement was once widely used. Unfortunately code that used goto extensively was poorly structured and could easily become unmanageable, and is known as "spaghetti code". Today, programming languages provide other ways to structure code so goto is rarely needed. In C++ it is always possible to use something other than goto to achieve what you need.

    Note that the use of goto is a religious issue and has provoked a great deal of debate. In C++ there is always a better alternative for writing loops, so you don't need to use goto at all. There are other circumstances where goto may be more appropriate than the alternatives, though that is a seperate subject.
    Quote Originally Posted by BrownB
    For example, I can use it to jump directly to a common exit part of a function from any place in the function where I catch an error...
    » Like previously stated, there is always a better alternative to the goto statement. Keep in mind when you use it to exit a block which contains aggregates requiring destructors, the destructors will run before the goto transfers control. The compiler still forbids using goto to enter a scope that requires constructors.

    The indiscriminate use of goto statements has caused tangled, miserable, impossible-to-read programs. To avoid the use of goto, more sophisticated, tightly controlled looping commands have been introduced: for, while, and do-while. Using these makes programs more easily understood, and goto is generally avoided, but one might argue that the case has been a bit overstated. Like any tool, carefully used and in the right hands, goto can be a useful construct, and the ANSI committee decided to keep it in the language because it has its legitimate uses.
    Quote Originally Posted by BrownB
    Oh yes, another curiosity: how does the "rating" work in these forums?
    » Some feel that the reputation aspect is not much more than just a fun little addition. It has nothing to do with your real status on the board. It's just for readers to see how reliable your response is.

    The Reputation System is not something I would stress over.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Psst, Stack: We're on the C board here. No C++, deconstructors, etc.
    If you understand what you're doing, you're not learning anything.

  11. #11
    I know,

    It's best I give the whole explanation than just half of it.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  12. #12
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    does anyone see anything wrong with this code?
    Code:
    void main(char **c, int i)
    {
        int x;
        if(x)
           x = 0;
       else 
          goto label;
      label:
    
      __asm mov eax, x
    }
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You mean besides the bassackwards main() definition? Or were you just referring to the sheer uselessness of the entire program?
    If you understand what you're doing, you're not learning anything.

  14. #14
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    uh, the bassackwardsness
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  15. #15
    Registered User
    Join Date
    Oct 2003
    Posts
    106
    Thank you all for your replies on this basic "quasi-philosophic" question. My point of view is this: I think that a program structure should be set as a study before coding, and so the goto statement / tecnique is just a tool, not a structure choice: the compiled asm code has many jmps, and it's well structured. Aren't jmps logically the same as gotos? (I'm speaking about C programming => no unknown contructor/destructor invoking precedence )

    The problem is in the maintenance / upgrade of the code, I agree. But what about the situation below:
    Code:
    - opening a database; error on open? goto Error
    - reading database ( so database is open ); error on Read? goto ErrorClose;
    - working with data; on error goto ErrorClose;
    - writing data; on error goto ErrorClose;
    
    ErrorClose:
    Close database file;
    
    Error:
    return errorCode;
    I think this isn't problematic, and it's clearer and easier to maintain than repeating a "database close" and returning on each error catched.

    If I'm in error (my degree isn't in Information Technology ) I beg your pardon

    BrownB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Curiosity question...
    By Raigne in forum C++ Programming
    Replies: 2
    Last Post: 04-05-2007, 09:49 PM
  2. For me is very important Please...
    By pitkini in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2005, 02:49 AM
  3. What Is Important In Making Games?
    By Krak in forum C++ Programming
    Replies: 6
    Last Post: 05-07-2004, 08:12 PM
  4. Planned Development Community Idea (important)
    By cozman in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 06-12-2002, 12:13 PM
  5. Most important element of a game?
    By Eber Kain in forum Game Programming
    Replies: 24
    Last Post: 01-11-2002, 03:04 PM