[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

Re: why goto ?



Hi Anurag,
Goto is mainly used in functions where having multiple return points is not
possible.What I mean that u simply cannot afford to have 2 return statements
in the same function.If the function needs to dom some cleanup before it
exits -for eg,to release a resource  it is holding -in the best cas it will
contain duplicated code.
U will find when u handle exceptions in C.
Let's  say u have a piece f code
void test()
{
    lock();//this will prevent any body from using a particular rsource .
    funA(status);
    EXCEPA(status);
    funB(status);
    EXCEPB(status);
EXIT:
      unlock();
}
Now consider the above program,The macros
excepA(status) and excepB(status) are exception handler's.Now when on
calling
function A suppose it returns an exception.Now EXCEPA(status) should write
the
error code to some file and then execute the statement
goto EXIT;
It cannot just return to the parent function without giving back all the
resources .If it had
done like that then that resource which funA used would never be used
again!.So each
function must have a single exit point.For more info read the  C/C++ user's
journal
Feb 2001 edition.
Thanx,
Harishankkar
----- Original Message -----
From: Anurag Shekhar <anurag@xxxxxxxxxxx>
To: <linux-india-programmers@xxxxxxxxxxxxxxxxxxxxx>
Sent: Friday, March 09, 2001 12:26 PM
Subject: [LIP] why goto ?


> i was going through the kernel modules and saw lots of goto (in fact
> this is the only place i have seen goto in any c code) And Alessandro
> Rubini (Linux Device Driver) also recommends using goto for error
> handling in kernel modules. Is there any specific advantage of using it
> ?
> thanks
> anurag
>
>
> ---------------------------------------------
> Find out more about this and other Linux India
> mailing lists at http://lists.linux-india.org/
>