Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 29th, 2005, 9:25 AM   #21
L7Sqr
Hobbyist Programmer
 
Join Date: Jun 2005
Location: here
Posts: 137
Rep Power: 0 L7Sqr is an unknown quantity at this point
Quote:
The status flag solution is often uglier and harder to understand than a goto.
Not true.
I would like to see a section of code that demonstrates this, if you have an example in mind....


Quote:
But, sometimes on a heavily constrained system, that status flag takes up too much space, or a function takes too much space or time.
If you are working on a system where an integer/boolean flag is too 'heavy' you will likely be on a level lower than C/C++ and the use of the goto becomes less relevant.....


If you ever have to sort through code that is poorly designed, you will form some pretty heavy opinions.
I have had experience with some code that uses goto for all flow control and (even with vi) it was a headache to follow.
I have seen something worse, however, code designed entirely out of #define macros. Tens of thousands of lines of code all revolving around macros. It took months to make a weeks worth of progress with the API. It worked fine if you wanted to just 'plug it in' but if you needed to adapt it (like we had to), you were up the creek, as it were...

goto and #define, they both give me a headache.
__________________
"...and though our kids are blessed their parents let them shoulder all the blame."
- The Quiet Things That No One Ever Knows [BrandNew]
L7Sqr is offline   Reply With Quote
Old Sep 29th, 2005, 11:20 AM   #22
Lesliect6
Programmer
 
Join Date: Aug 2005
Posts: 68
Rep Power: 4 Lesliect6 is on a distinguished road
Thank you guys! Then it's maybe because of that that my qaudratic eqaution solver is running at 100 percent CPU...

_____________________________________
www.freewebs.com/lesliect6
Lesliect6 is offline   Reply With Quote
Old Sep 29th, 2005, 12:02 PM   #23
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Or it could be because DarkBASIC is designed for games, and it therefore assumes it can use the entire CPU - after all, who plays a game and does other stuff?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Sep 29th, 2005, 12:49 PM   #24
Scorpions4ever
Programmer
 
Join Date: Jun 2005
Posts: 86
Rep Power: 4 Scorpions4ever is on a distinguished road
Actually, there are still good uses for goto in C++. In fact, you can use GOTOs to write clean code as well, provided you use them sparingly. Say you are allocating resources as you go within a function and you hit an error condition. You can either put blocks of duplicated code to free previously allocated resources, or you can neatly hop out with a goto and have all your deallocation code in one place.

First, the code without any goto calls:
int SomeFunc() {
    SomeType *x1 = NULL, *x2 = NULL, *x3 = NULL, *x4 = NULL;

    if ((x1 = alloc_resource1()) == NULL) {
        return -1;        
    }

    if ((x2 = alloc_resource2()) == NULL) {
        free_resource1(x1);
        return -2;        
    }

    if ((x3 = alloc_resource3()) == NULL) {
        free_resource1(x1);
        free_resource2(x2);
        return -3;        
    }

    if ((x4 = alloc_resource4()) == NULL) {
        free_resource1(x1);
        free_resource2(x2);
        free_resource3(x3);
        return -4;        
    }
   // Now do something with x1, x2, x3 and x4

   // Now free everything on the way out
   free_resource1(x1);
   free_resource2(x2);
   free_resource3(x3);
   free_resource4(x4);
   return SUCCESS;
}
Note how ugly the above code is and also the number of duplicated statements to handle error conditions. On top of that, if you add an extra allocated resource somewhere in between, you'll need to cascade the free_resourceX() calls all the way down, which is tedious and there is a chance that you may miss the call in one block somewhere, which means maintenance is a pain in the neck.

Now, the same code with goto
int SomeFunc() {
    SomeType *x1 = NULL, *x2 = NULL, *x3 = NULL, *x4 = NULL;
    int ret_code = SUCCESS;

    if ((x1 = alloc_resource1()) == NULL) {
        ret_code = -1;
        goto cleanup;
    }

    if ((x2 = alloc_resource2()) == NULL) {
        ret_code = -2;
        goto cleanup;
    }

    if ((x3 = alloc_resource3()) == NULL) {
        ret_code = -3;
        goto cleanup;
    }

    if ((x4 = alloc_resource4()) == NULL) {
        ret_code = -4;
        goto cleanup;
    }

    // Now do something with x1, x2, x3 and x4

   // Now free everything on the way out
cleanup:
   free_resource1(x1);
   free_resource2(x2);
   free_resource3(x3);
   free_resource4(x4);
   return ret_code;
}
Notice how clean the code has become. It also promotes the one entry-one exit style of coding. Code maintenance is also a breeze because all your deallocation is done at one spot. So, if you had to allocate x5, you don't have to cascade the free_resource5() calls unlike the previous code with no goto statements.

BTW, if you want a real example instead of pseudo-code, try your hand out at COM programming. This paradigm is pretty common there. Also, the linux kernel uses the same idea (well, the kernel is C instead of C++, but the same idea applies). Do a grep for "goto" in the kernel source and you'll see plenty of instances of "goto out;", "goto out_unlock;", "goto leave;" etc.
Scorpions4ever is offline   Reply With Quote
Old Sep 29th, 2005, 1:19 PM   #25
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Scorpion's point is a valid one. Note that you may be able to accomplish the same thing with properly used exceptions, if you're working in C++.

Goto is not evil. People use it stupidly or unthinkingly.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 29th, 2005, 3:19 PM   #26
Scorpions4ever
Programmer
 
Join Date: Jun 2005
Posts: 86
Rep Power: 4 Scorpions4ever is on a distinguished road
Exceptions can be considered somewhat like super-gotos, since they can span across multiple levels of functions. The trouble with exceptions is that not every object throws them and Stroustrup himself recommends that you don't use Exceptions when other alternatives are possible. Exceptions should really be used to signal errors that cannot be handled locally.

Speaking of resource allocations and exceptions with C++, the best paradigm for this is to use RAII (Resource Allocation Is Initialization). What this means is that the constructor is responsible for trying to acquire the resource and should throw an exception if it can't do so.
class X {
    private:
       Sometype *ptr;
    public:
         X() {
            if ((ptr = alloc_resource()) != NULL)
                throw alloc_failed();
         }
        
        ~X() {
            if (ptr != NULL)
                free_resource(ptr);
        } 
};

Then, this makes the above example with gotos so much simpler, since you can just do this:
int SomeFunc() {
    int ret_code = SUCCESS;
    try {
         X x1, x2, x3, x4;
         // Do something with x1, x2, x3 and x4.
    }
    except (alloc_failed &a) {
        ret_code = -1;
    }

    return ret_code;
}
Note here that the exception is thrown because the error handling cannot be taken care of in the constructor. Also note that there is no need to have special code to free the resources since the destructor is automatically called when the object falls out of scope. Note that the only reason I used a try...except block in the above was to return an error code. I could easily have omitted the try...except block within the function.
void SomeFunc() {
   X x1, x2, x3, x4;
   // Do something here with x1, x2, x3 and x4
}
and if there is an exception thrown, C++ still guarantees that x1, x2, x3, and x4 will be freed on the way out.

The code using gotos is normally used when you're mixing C-style resource allocating functions with C++ and don't want to write wrapper classes to encapsulate the resource allocation bits.
Scorpions4ever is offline   Reply With Quote
Old Sep 29th, 2005, 4:19 PM   #27
Scorpions4ever
Programmer
 
Join Date: Jun 2005
Posts: 86
Rep Power: 4 Scorpions4ever is on a distinguished road
Replace try...except() with try...catch() in all the above. That's what I get for switching programming languages back and forth.
Scorpions4ever is offline   Reply With Quote
Old Sep 29th, 2005, 4:32 PM   #28
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
i also don't think you can "throw" errors in C++.
__________________

tempest is offline   Reply With Quote
Old Sep 29th, 2005, 4:34 PM   #29
Scorpions4ever
Programmer
 
Join Date: Jun 2005
Posts: 86
Rep Power: 4 Scorpions4ever is on a distinguished road
Quote:
Originally Posted by tempest
i also don't think you can "throw" errors in C++.
throw is the C++ keyword to throw an exception. Mercifully, I didn't screw that one up .
Scorpions4ever is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:59 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC