Discussion in "General help Guidance and Discussion" started by    DavesGarage    Nov 6, 2009.
Fri Nov 06 2009, 09:48 PM
#1
At the behest of our website owner, I decided to open a forum thread on software programming techniques. I have been writing software for about 25 years now, and I find I am constantly learning new and innovative ways to get into trouble with my code...

I thought this could be a place where we discuss specific topics - this particular thread is dedicated to the use of GOTO, which is defined within K&R's "The C Programming Language" book as follows:

3.8 Goto and labels
C provides the infinitely-abusable goto statement, and labels to branch to. Formally, the goto statement is never necessary, and in practice it is almost always easy to write code without it. We have not used goto in this book. 
Nevertheless, there are a few situations where gotos may find a place. The most common is to abandon processing in some deeply nested structure, such as breaking out of two or more loops at once. The break statement cannot be used directly since it only exits from the innermost loop. Thus: 

       for ( ... )
           for ( ... ) {
               ...
               if (disaster)
                   goto error;
           }
       ...
   error:
       /* clean up the mess */
This organization is handy if the error-handling code is non-trivial, and if errors can occur in several places. 
A label has the same form as a variable name, and is followed by a colon. It can be attached to any statement in the same function as the goto. The scope of a label is the entire function. 
As another example, consider the problem of determining whether two arrays a and b have an element in common. One possibility is 

       for (i = 0; i < n; i++)
           for (j = 0; j < m; j++)
               if (a[i] == b[j])
                   goto found;
       /* didn't find any common element */
       ...
   found:
       /* got one: a[i] == b[j] */
       ...
Code involving a goto can always be written without one, though perhaps at the price of some repeated tests or an extra variable. For example, the array search becomes 

   found = 0;
   for (i = 0; i < n && !found; i++)
       for (j = 0; j < m && !found; j++)
           if (a[i] == b[j])
               found = 1;
   if (found)
       /* got one: a[i-1] == b[j-1] */
       ...
   else
       /* didn't find any common element */
       ...
With a few exceptions like those cited here, code that relies on goto statements is generally harder to understand and to maintain than code without gotos. Although we are not dogmatic about the matter, it does seem that goto statements should be used rarely, if at all. 



Using GOTO in your software is a widely debated issue. What are your thoughts?

Sat Nov 07 2009, 10:06 AM
#2
I believe if you know what you are doing then its not a problem using goto statement. I sometimes really feel difficult to understand why people abuse so much?

If goto can make you life simple then why not use it? I am not a master in programming but i feel wherever you can use goto just use it.

I have seen uIP 0.9 implementation by Adam Dunkels, he made a heavy use of goto statement just to make things simpler, he could also use switch case statements but he decided to use goto.

Can anyone points to problem that can arise using goto?
Fri Nov 13 2009, 11:17 PM
#3
Searching the web, I ran across a very good description of when to use goto...

The author wrote:

When To Use Goto When Programming in C
Although the use of goto is almost always bad programming practice (surely you can find a better way of doing XYZ), there are times when it really isn't a bad choice. Some might even argue that, when it is useful, it's the best choice.

Most of what I have to say about goto really only applies to C. If you're using C++, there's no sound reason to use goto in place of exceptions. 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.

What do I mean? You might have some code that looks like this:

int big_function()
{
    /* do some work */
    if([error])
    {
        /* clean up*/
        return [error];
    }
    /* do some more work */
    if([error])
    {
        /* clean up*/
        return [error];
    }
    /* do some more work */
    if([error])
    {
        /* clean up*/
        return [error];
    }
    /* do some more work */
    if([error])
    {
        /* clean up*/
        return [error];
    }
    /* clean up*/
    return [success];
}

This is fine until you realize that you need to change your cleanup code. Then you have to go through and make 4 changes. Now, you might decide that you can just encapsulate all of the cleanup into a single function; that's not a bad idea. But it does mean that you'll need to be careful with pointers -- if you plan to free a pointer in your cleanup function, there's no way to set it to then point to NULL unless you pass in a pointer to a pointer. In a lot of cases, you won't be using that pointer again anyway, so that may not be a major concern. On the other hand, if you add in a new pointer, file handle, or other thing that needs cleanup, then you'll need to change your cleanup function again; and then you'll need to change the arguments to that function.

In some cases, this might be acceptable -- but if you're making a lot of changes to your code, adding in new variables, etc. -- it may not be worth the time or the extra lines of code to make that function call.

Instead, since you know that you're going to be executing only one piece of code and then returning from the function, you might as well use a goto to jump to the very end of the function, where you have your cleanup code.

Goto is a pretty simple keyword: you just need to include a "label" placed above the target location (followed by a colon), and then direct the program to go to the label. Note that this only works within the same function; you can't just enter one function from another.

goto label;
/* Code
...
*/
label:

Now, using this, if we want to handle all our errors in one place, we'll need to add in a variable to track the return value of our function so we can return it:

int big_function()
{
    int ret_val = [success];
    /* do some work */
    if([error])
    {
        ret_val = [error];
        goto end;
    }
    /* do some more work */
    if([error])
    {
        ret_val = [error];
        goto end;
    }
    /* do some more work */
    if([error])
    {
        ret_val = [error];
        goto end;
    }
    /* do some more work */
    if([error])
    {
        ret_val = [error];
        goto end;
    }
end:
    /* clean up*/
    return ret_val;
}

The benefit here is that your code following end has access to everything it will need to perform cleanup, and you've managed to reduce the number of change points considerably. Another benefit is that you've gone from having multiple exit points for your function to just one; there's no chance you'll accidentally return from the function without cleaning up.

Moreover, since goto is only being used to jump to a single point, it's not as though you're creating a mass of spaghetti code jumping back and forth in an attempt to simulate function calls. Rather, goto actually helps write more structured code.

There is one thing to be aware of: while your cleanup code should be able to free all of the memory you use, there may be times when you actually want to free that memory yourself and possibly reallocate it later. In these cases, if you do call free on a ptr and then have an if([error]) between that call to free and the subsequent call to malloc, you should definitely set the pointer to point to NULL! This will prevent your jumping to the cleanup code and then calling free on that pointer a second time, which can result in a security hole (the "double free" problem).

Goto should always be used sparingly, and as a last resort -- but there is a time and a place for it. The question should be not "do you have to use it" but "is it the best choice" to use it.



The website is here: http://www.cprogramming.com/tutorial/goto.html


[ Edited Fri Nov 13 2009, 11:19 PM ]
 ajay_bhargav like this.
Sat Nov 14 2009, 06:08 AM
#4
That explains a lot
I really like this method of spreading and gathering good knowledge
Mon Nov 16 2009, 10:19 AM
#5


I believe if you know what you are doing then its not a problem using goto statement. I sometimes really feel difficult to understand why people abuse so much?

If goto can make you life simple then why not use it? I am not a master in programming but i feel wherever you can use goto just use it.

I have seen uIP 0.9 implementation by Adam Dunkels, he made a heavy use of goto statement just to make things simpler, he could also use switch case statements but he decided to use goto.

Can anyone points to problem that can arise using goto?

Ajay Bhargav



The goto statement has run me into alot of problems when I started coding in embedded C. I sometimes find that using goto before closing a while(1) loop can cause 18F series PIC chips to crash. Goto can be used to make things much simpler but I personally think it is down to the programmer. While working on projects in different teams, I find that some programmers code in a style that is similar to the language they started out with.

I've noticed one guy who started out coding in assembly, his C code looks very similar in structure!
Mon Nov 16 2009, 02:39 PM
#6
banked memories may cause a problem with goto i believe. 8051 is fairly simple architecture where you can jump from anywhere in range of 64K. So architectures also play big role on how you code.

but most of time Its on user how he use it or code it.
Mon Nov 16 2009, 04:52 PM
#7


The goto statement has run me into alot of problems when I started coding in embedded C. I sometimes find that using goto before closing a while(1) loop can cause 18F series PIC chips to crash.

Retellect


I suspect that the compiler may be careless as to how it codes while loops.
Useful warning though.


[ Edited Mon Nov 16 2009, 04:53 PM ]

Get Social

Information

Powered by e107 Forum System

Conversation

Wed Mar 25 2020, 09:39 AM
Tajammul
Sir kindly send the code file i2c for 8051
ExperimenterUK
Fri Mar 13 2020, 06:36 PM
@ Rohitnani,ABEBE..which project ?
Fri Mar 13 2020, 08:55 AM
ABEBE
how can get hadwere of this project
Rohitnani
Sat Mar 07 2020, 12:11 PM
Sir, Could you please send me the zip file input password....
Wed Feb 26 2020, 11:58 AM
Peter Parker
Is it possible to send multiple messages using only 1 GSM module?
Sat Feb 01 2020, 05:12 AM
Pearl Olin
Hello,
Wed Jan 22 2020, 01:16 PM
MINU CHAUDHARY
I m getting errors while simulation process during interfacing socket output and lcd... Can i get the report of ur project which is same as us.
Tue Jan 14 2020, 02:08 AM
shubham kharade
when i am trying to simulate the program in Proteus 8 professional, I am getting some errors as cannot find module file dhtxx.md can u help me
Tue Dec 17 2019, 08:44 AM
Devangi
hello..i am programming industrial timer using 8051 with 24lc64 eeprom. i have query in storing the 3 diff value in eeprom with enter button. can anyone share c code ?
Tue Dec 17 2019, 05:30 AM
Andrius
Hello. Can anyone share the code in Bascon and scheme in Proteus?

Downloads

Comments

ElizabethJuich
Wed Apr 01 2020, 09:24 AM
MichaelVaf
Tue Mar 31 2020, 07:49 AM
Antonduh
Tue Mar 31 2020, 12:58 AM
AndreySem
Mon Mar 30 2020, 01:35 AM
vityaCar
Sat Mar 28 2020, 11:21 PM
DeloresGrinc
Fri Mar 27 2020, 03:27 AM
Alexeyatola
Fri Mar 27 2020, 02:37 AM
RitaCar
Wed Mar 25 2020, 01:19 PM

Online

Guests: 50, Members: 0 ...

most ever online: 182184
(Members: , Guests: 182184) on 06 Aug 2010: 01:37 AM

Members: 38139
Newest member: ElizabethJuich
Forum Activity
PHANTOM28 Avatar
Posted By PHANTOM28
Fri Feb 28 2020, 10:25 AM
Hi.I hope you all can help me. I am relatively ne...
ExperimenterUK Avatar
Posted By ExperimenterUK
Fri Feb 21 2020, 05:05 PM
ajay_bhargav Avatar
Posted By ajay_bhargav
Fri Feb 21 2020, 04:45 AM
Yes you can do that.1. Create a channel @Thingspea...
ananyagupta Avatar
Posted By ananyagupta
Thu Feb 20 2020, 03:58 AM
With regards to the particular characters sent to ...
sarrahjuzar Avatar
Posted By sarrahjuzar
Wed Feb 19 2020, 04:24 AM
I have a gsm module sim900a and dht22 connected to...
ExperimenterUK Avatar
Posted By ExperimenterUK
Tue Feb 18 2020, 04:52 PM
figureyang Avatar
Posted By figureyang
Tue Feb 18 2020, 04:59 AM
figureyang Avatar
Posted By figureyang
Tue Feb 18 2020, 04:50 AM
wounder Avatar
Posted By wounder
Mon Feb 10 2020, 03:32 AM
From below sample code for nuvoton 8051 timer0 mod...
Just4Fun Avatar
Posted By Just4Fun
Sat Feb 01 2020, 09:20 AM
If you want to use a CH55x MCU there isn't any adv...