![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#21 | ||
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 137
Rep Power: 0
![]() |
Quote:
I would like to see a section of code that demonstrates this, if you have an example in mind.... Quote:
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] |
||
|
|
|
|
|
#22 |
|
Programmer
Join Date: Aug 2005
Posts: 68
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#23 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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?
|
|
|
|
|
|
#24 |
|
Programmer
Join Date: Jun 2005
Posts: 86
Rep Power: 4
![]() |
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;
}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;
}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. |
|
|
|
|
|
#25 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#26 |
|
Programmer
Join Date: Jun 2005
Posts: 86
Rep Power: 4
![]() |
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;
}void SomeFunc() {
X x1, x2, x3, x4;
// Do something here with x1, x2, x3 and x4
}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. |
|
|
|
|
|
#27 |
|
Programmer
Join Date: Jun 2005
Posts: 86
Rep Power: 4
![]() |
Replace try...except() with try...catch() in all the above. That's what I get for switching programming languages back and forth.
|
|
|
|
|
|
#28 |
|
Programming Guru
![]() |
i also don't think you can "throw" errors in C++.
__________________
|
|
|
|
|
|
#29 | |
|
Programmer
Join Date: Jun 2005
Posts: 86
Rep Power: 4
![]() |
Quote:
. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|