473,806 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

goto

Hi,
I know I know its notoriously bad! I don't want to use it. I was wondering,
does it still exist? If it does, I really don't understand how!? like what
happens if you just goto halfway through a function (no objects properly
constructed!) , or a constructor itself??
Just intrigued!!

Mike
Jul 23 '05
36 6742
On 2005-03-28 06:58:54 -0500, Chris Croughton <ch***@keristor .net> said:
On Mon, 28 Mar 2005 04:24:31 +0300, Ioannis Vranos
<iv*@remove.thi s.grad.com> wrote:

I can understand the use of goto for the situations that I described.
For example, in application programming, in non-desperate for speed
conditions, why should one use goto?


For clarity when appropriate.

void func(...)
{
for (...)
{
for (...)
{
for (...)
{
/* do stuff */
if (something_wron g)
goto cleanup;
/* do more stuff */
if (something_wron g)
goto cleanup;
}
}
}
cleanup:
/* do cleanup stuff */
}

It's a lot clearer than using flags to do it, and return isn't
appropriate because the 'cleanup' code would need to be duplicated which
is a maintenabce risk.


What about this (no duplication, exception safe, no potential for goto
jumping across constructors/destructors, etc.):

void func(...)
{
struct cleanup {
~cleanup() { /* do cleanup stuff */ }
} cleaner;

for (...)
{
for (...)
{
for (...)
{
/* do stuff */
if (something_wron g)
return; /* or throw*/

/* do more stuff */
if (something_wron g)
return; /* or throw*/
}
}
}
}

--
Clark S. Cox, III
cl*******@gmail .com

Jul 23 '05 #31
Clark S. Cox III wrote:
What about this (no duplication, exception safe, no potential for goto
jumping across constructors/destructors, etc.):

void func(...)
{
struct cleanup {
~cleanup() { /* do cleanup stuff */ }
} cleaner;

for (...)
{
for (...)
{
for (...)
{
/* do stuff */
if (something_wron g)
return; /* or throw*/

/* do more stuff */
if (something_wron g)
return; /* or throw*/
}
}
}
}


Try to pass local variables into ~cleanup(), to reliably clean them up. Your
little class rapidly gets very ugly - it grows a constructor, members, etc.

RAII works best when handles destruct themselves. Put another way,
destruction is an encapsulated detail of the handle, not of the function
using the handle.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 23 '05 #32
Ioannis Vranos wrote:
So if you want to establish a simple network connection for example and
it fails, it throws an exception. It doesn't return a null pointer. And
I think this is the right approach.

Actually, it does not throw an exception on connection failure but a positive integer is
returned in connection success, and 0 in network failure.

However for all unexpected errors an exception is thrown, like when trying to open a file
with write access while it is being used by another application, for example.
--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #33
Ioannis Vranos wrote:
So if you want to establish a simple network connection for example
and it fails, it throws an exception. It doesn't return a null
pointer. And I think this is the right approach.


Actually, it does not throw an exception on connection failure but a
positive integer is returned in connection success, and 0 in network
failure.

Actually I think it is a mistake of the book I am reading, it *does* throw an exception.
http://msdn.microsoft.com/library/en...necttopic2.asp

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #34
Phlip wrote:
Try to pass local variables into ~cleanup(), to reliably clean them up. Your
little class rapidly gets very ugly - it grows a constructor, members, etc.

RAII works best when handles destruct themselves. Put another way,
destruction is an encapsulated detail of the handle, not of the function
using the handle.


I prefer...

int cleanup_variabl e = 0; // Local vars needed for cleanup

class my_exception {};

try {
for (...)
{
for (...)
{
for (...)
{
// ...
if (something_wron g)
throw my_exception;

// ...
if (something_wron g)
throw my_exception;
}
}
}
} catch (my_exception&) {
// cleanup
// Optionally rethrow another exception
}

With a good compiler and optimizer, the run-time behavior of this code
should be equivalent to a goto to the outer block, with the additional
bonus that destructors for objects created in the for scope will be
properly called on the way out.

-dr
Jul 23 '05 #35
Dave Rahardja wrote:
Phlip wrote:
Try to pass local variables into ~cleanup(), to reliably clean them up. Your little class rapidly gets very ugly - it grows a constructor, members, etc.
RAII works best when handles destruct themselves. Put another way,
destruction is an encapsulated detail of the handle, not of the function
using the handle.


I prefer...

int cleanup_variabl e = 0; // Local vars needed for cleanup

class my_exception {};

try {
for (...)
{
for (...)
{
for (...)
{
// ...
if (something_wron g)
throw my_exception;

// ...
if (something_wron g)
throw my_exception;
}
}
}
} catch (my_exception&) {


catch(...), so called functions can throw safely.
// cleanup
// Optionally rethrow another exception
}

With a good compiler and optimizer, the run-time behavior of this code
should be equivalent to a goto to the outer block, with the additional
bonus that destructors for objects created in the for scope will be
properly called on the way out.


However, RAII works best when handles destruct themselves. Put another way,
destruction is an encapsulated detail of the handle, not of the function
using the handle.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 23 '05 #36
Dave Rahardja wrote:
I prefer...

int cleanup_variabl e = 0; // Local vars needed for cleanup

class my_exception {};

try {
for (...)
{
for (...)
{
for (...)
{
// ...
if (something_wron g)
throw my_exception;

// ...
if (something_wron g)
throw my_exception;
}
}
}
} catch (my_exception&) {
// cleanup
// Optionally rethrow another exception
}

With a good compiler and optimizer, the run-time behavior of this code
should be equivalent to a goto to the outer block, with the additional
bonus that destructors for objects created in the for scope will be
properly called on the way out.

A style that I saw in a .NET book recently. I am just posting it here to see comments. :-)
for(...)
{
try
{
// Network connection attempt with object in the managed heap
}

catch(Exception *)
{
break;
}
}

I guess this makes sense for garbage collected objects in the heap.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #37

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
4281
by: Hayri ERDENER | last post by:
hi, what is the equivalent of C languages' goto statement in python? best regards
51
13393
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this wrong????? Function x select case z
37
3262
by: Tim Marshall | last post by:
From http://www.mvps.org/access/tencommandments.htm 9th item: Thou shalt not use "SendKeys", "Smart Codes" or "GoTo" (unless the GoTo be part of an OnError process) for these will lead you from the path of righteousness. What about also using it as a means of exiting a procedure?
52
21986
by: Rick | last post by:
Hi, For portability, can an issue arise if we use while(1) for an infinite loop in C? If so, should we then use for(;;)? Thanks, Rick
45
2735
by: Debashish Chakravarty | last post by:
K&R pg.66 describes two situations when using goto makes sense. Has anyone here come across situations where using goto provided the most elegant solution. -- http://www.kashmiri-pandit.org/atrocities/index.html
77
4043
by: M.B | last post by:
Guys, Need some of your opinion on an oft beaten track We have an option of using "goto" in C language, but most testbooks (even K&R) advice against use of it. My personal experience was that goto sometimes makes program some more cleaner and easy to understand and also quite useful (in error handling cases). So why goto is outlawed from civilized c programmers community. is there any technical inefficiency in that.
34
26651
by: electrician | last post by:
Perl has it, Basic has it, Fortran has it. What is so difficult about creating a goto command for JavaScript. Just set up a label and say go to it.
3
2195
by: electrician | last post by:
Yes, no GOTO. This is a major blunder on part of the creators of these tools. GOTO gives the programmer the absolute control over the program. Yes, no matter what, a GOTO sends the program to where YOU want and takes the power away from the mean spirited CREATORS. Yes, even GOD gave us free will! But the creators of JavaScript have decided they will forever interfere in how YOU program. THEY have decided to keep GOTO to themselves...
17
2621
by: SoftEast | last post by:
Hi Buddies, I have read a lot of stuffs regarding not using GOTO statements to opt a good programming style http://david.tribble.com/text/goto.html]. Can anybody give a particular lines of code which shows harmfullness of GOTO. SoftEast...
59
5080
by: raashid bhatt | last post by:
why are GOTO's not used they just a simple JMP instructions what's bad about them
0
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10371
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4330
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3853
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3010
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.