Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Valid use of goto for error management in C?

This question is actually a result of an interesting discussion at programming.reddit.com a while ago. It basically boils down to the following code:

int foo(int bar) {     int return_value = 0;     if (!do_something( bar )) {         goto error_1;     }     if (!init_stuff( bar )) {         goto error_2;     }     if (!prepare_stuff( bar )) {         goto error_3;     }     return_value = do_the_thing( bar ); error_3:     cleanup_3(); error_2:     cleanup_2(); error_1:     cleanup_1();     return return_value; } 

The usage of goto here appears to be the best way to go, resulting in the cleanest and most efficient code of all possibilities, or at least so it seems to me. Quoting Steve McConnell in Code Complete:

The goto is useful in a routine that allocates resources, performs operations on those resources, and then deallocates the resources. With a goto, you can clean up in one section of the code. The goto reduces the likelihood of your forgetting to deallocate the resources in each place you detect an error.

Another support for this approach comes from the Linux Device Drivers book, in this section.

What do you think? Is this case a valid use for goto in C? Would you prefer other methods, which produce more convoluted and/or less efficient code, but avoid goto?

like image 425
Eli Bendersky Avatar asked Apr 25 '09 13:04

Eli Bendersky


People also ask

What is the use of goto in C?

The goto statement allows us to transfer control of the program to the specified label .

What is the use of goto operator?

The goto statement can be used to alter the flow of control in a program. Although the goto statement can be used to create loops with finite repetition times, use of other loop structures such as for, while, and do while is recommended. The use of the goto statement requires a label to be defined in the program.

Why goto is not recommended to use in programs?

NOTE − Use of goto statement is highly discouraged in any programming language because it makes difficult to trace the control flow of a program, making the program hard to understand and hard to modify. Any program that uses a goto can be rewritten to avoid them.

Is goto a good practice in C?

In C, however, you don't have the power of an exception handling mechanism, so if you want to separate out error handling from the rest of your program logic, and you want to avoid rewriting clean up code multiple times throughout your code, then goto can be a good choice.


1 Answers

FWIF, I find the error handling idiom you gave in the question's example to be more readable and easier to understand than any of the alternatives given in the answers so far. While goto is a bad idea in general, it can be useful for error handling when done in a simple and uniform manner. In this situation, even though it's a goto, it's being used in well-defined and more or less structured manner.

like image 163
Michael Burr Avatar answered Oct 21 '22 20:10

Michael Burr