goto

This is a discussion on goto within the A Brief History of Cprogramming.com forums, part of the Community Boards category; thought: say you had code such as this... NOTE: using C... Code: object* factory(parameter1, parameter2, para....) { object* a = ...

  1. #1
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267

    goto

    thought:

    say you had code such as this...

    NOTE: using C...

    Code:
    
    object* factory(parameter1, parameter2, para....)
    {
       object* a = allocate memory;
    
       if(!a)
          return NULL;
    
       a->member1 = allocate memory based on parameter1;
       if(!a->member1)
       {
          free(a);
          return NULL;
       }
    
       a->member2 = allocate memory based on parameter2;
       if(!a->member2)
       {
          free(a);
          free(a->member1);
       }
    
    /// etc................
        return a;
    }
    error handling code is redundant... and continually grows longer....

    solution 1:

    Code:
    void FAIL(object* obj)
    {
        free(obj->member2);
        free(obj->member1);
    //    etc....
        free(obj);
    }
    
    object* factory(parameter1, parameter2, para....)
    {
       object* a = allocate memory zeroed memory;
    
      if(!a)
          return NULL;
    
      if(!(a->member1 = allocate memory based on parameter1))
      {
          FAIL(a);
          return NULL;
      }
    
      if(!(a->member2 = allocate memory based on parameter2))
      {
          FAIL(a);
          return NULL;
      }
    
    
    /// etc................
    
        return a;
    }
    requires a specialized function... but it works and is cleaner...

    solution 2:

    Code:
    
    object* factory(parameter1, parameter2, para....)
    {
       object* a = allocate memory zeroed memory;
    
      if(!a)
          return NULL;
    
      if(!(a->member1 = allocate memory based on parameter1))
           goto FAIL;
    
      if(!(a->member2 = allocate memory based on parameter2))
           goto FAIL;
    
    /// etc................
    
        return a;
    
        FAIL:
        free(a->member2);
        free(a->member1);
     //   etc....
        free(a);
        return NULL;
    }
    cleaner requires no function... uses goto(OH NOES!!!!!)...

    comments... suggestions? thoughts?
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    That's a valid use of goto in my honest opinion.

    Alternatively, you could do:
    Code:
    object* factory(parameter1, parameter2, para....)
    {
       int success = true;
       object* a = allocate memory;
    
       if(!a)
         success = false;
    
       if(success)
       {
         a->member1 = allocate memory based on parameter1;
         if(!a->member1)
            success = false;
       }
    
       if(success)
       {
         a->member2 = allocate memory based on parameter2;
         if(!a->member2)
            success = false;
       }
    
    
       if( false == success && a)
       {
         free(a);
         a = NULL;
       }
       return a;
    }
    This allows for one to gracefully recover if necessary.

  3. #3
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    498
    What about exception handling? It may not be exactly what you are looking for, but I sure like it. The only disadvantage when you want to use exception handling is that it's much easier to write it in C++, so although C users can do it, classes allow you to take full advantage of exception handling.

    If you don't know what exception handling is, go here:
    http://www.flipcode.com/articles/art...handling.shtml


    Edit: this should have really been posted in the C forums
    Last edited by joeprogrammer; 05-17-2006 at 05:47 PM.

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    you disreguard many factors... firstly though the example is meant to be in C, it is quite valid in C++, and is really not even a language specific idea honestly... the example could very well be translated into many languages... the topic is not the language nor language specific solutions... exception handling is not available in C in any easy or simple means though it is the optimal solution in C++ if you disreguard the overhead associated with its use...
    the idea is meant to be clean with minimal overhead and minimal complexity to the reading or function of the code(and in C)... goto overall is the simplest cleanest easiest and quite probably the fastest solution to the problem hence it is an "argument" for its use as opposed to the function version, or the flag version... or even exception handling...
    in essence this is an argument for the use of goto... which is a multi-language tool.

    i posted it because im curious to know if i missed something... or is this infact the best solution... and it doesnt belong in the C forum, because its about goto and programming in general... not the c language
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >comments... suggestions? thoughts?
    That's one of the accepted uses of goto.
    My best code is written with the delete key.

  6. #6
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,070
    I use goto's in the same way, to skip for loops in my game engines rendering code depending on the result of a frustum culling test. Very useful, IMO.
    Memorial University of Newfoundland
    Computer Science

    Mac and OpenGL evangelist.

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    The nicest programming solution is to use nested if blocks. However, for a function with more than a couple of failure points the goto solution can produce far clearer code.
    Code:
      if((a->member1 = allocate memory based on parameter1))
      {
        if((a->member2 = allocate memory based on parameter2))
        {
          return a;
        }
      }
    
      // failed
    The goto solution is roughly semantically equivalent to exception handling, although exceptions can be a lot harder to debug and prove.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >The nicest programming solution is to use nested if blocks.
    Except when you consider that extreme levels of indentation hinder readability. Add two more parameters, then add the code that works with the resulting memory, and you'll see the problem. You'll probably also want to add else clauses to handle deallocation of unused memory, and since you're doing that, why not more detailed error messages? It gets complicated quickly when you do it right, and adding the nested blocks suddenly isn't such a nice solution.
    My best code is written with the delete key.

  9. #9
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Quote Originally Posted by Prelude
    >The nicest programming solution is to use nested if blocks.
    Except when you consider that extreme levels of indentation hinder readability. Add two more parameters, then add the code that works with the resulting memory, and you'll see the problem. You'll probably also want to add else clauses to handle deallocation of unused memory, and since you're doing that, why not more detailed error messages? It gets complicated quickly when you do it right, and adding the nested blocks suddenly isn't such a nice solution.
    Totally agree. I'm a big fan of the goto solution. Check here or here or here. I'd challenge anyone to come up with a better solution for any of those three examples.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  2. goto command
    By jhwebster1 in forum C Programming
    Replies: 3
    Last Post: 02-21-2006, 11:32 AM
  3. Does goto have a glitch or...?
    By Blackroot in forum C++ Programming
    Replies: 9
    Last Post: 02-18-2006, 09:40 AM
  4. helpppp
    By The Brain in forum C Programming
    Replies: 1
    Last Post: 07-27-2005, 07:05 PM
  5. 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

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