/* attempt to deal with prototype, bootstrap, jquery conflicts */ /* for dropdown menus */

Jump to content

Image of the Day

Today's #ScreenshotSaturday features Face Your Fears from Turtle Roc ... #virtualrealityshop #mobilevr #vrshop https://t.co/3qhSNAVNOZ
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net's newsletters to receive the latest updates and exclusive content.


Sign up now

C++ - Is Goto a Good Practice?

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
46 replies to this topic

#41 powly k   Members   

657
Like
-6Likes
Like

Posted 15 November 2012 - 09:22 AM

A small remark - the guys who dislike goto are on the forums whining about it while the guys who like goto are actually coding something.

Which do you want to be?

(EDIT: Another small remark - guys around here seem to have no clue about the concept of humour!)

Edited by powly k, 18 November 2012 - 03:40 AM.


#42 rip-off   Moderators   

10737
Like
0Likes
Like

Posted 15 November 2012 - 10:16 AM

... the guys who dislike goto are on the forums whining about it while the guys who like goto are actually coding something.

Wow. That is an impressively simplistic false dichotomy.

#43 powly k   Members   

657
Like
0Likes
Like

Posted 15 November 2012 - 06:07 PM

... or a bad joke :) And though I (seriously) don't think goto is bad by default, I've never had a reason to use it.

#44 Bacterius   Members   

13144
Like
1Likes
Like

Posted 15 November 2012 - 06:15 PM

I think one of the only times I ever used goto was to get out of a three-level nested loop. It wasn't final code and I couldn't be bothered to add break flags or put the loops in their own function, so I just used goto as an extended break. But I wouldn't recommend it - it's not really compatible with higher level control flow, but it's nice to know it exists.

I'm sure there are instances out there where goto may in fact be beneficial (someone mentioned cleaning up C functions, which is a good one, of course you can achieve the same goal by carefully nesting your allocations but sometimes goto is the only elegant way to do it), but you certainly don't use goto "just because you can".

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”


#45 Blewweh   Members   

107
Like
0Likes
Like

Posted 16 November 2012 - 07:55 AM

Say no to goto!~~

heehee.

#46 Codarki   Members   

462
Like
0Likes
Like

Posted 16 November 2012 - 01:51 PM

I don't actually know what happens in the following scenarios. Does goto respect rules of the scoped objects?
{
	std::string tee("hee");
	goto forward;
}
forward:
// what happens with destruction of tee? Memory leak I guess.
backward:
// what happens with destruction of tee? Memory leak I guess.
int i = 0;
if (i == 0)
{
	i = 1;
	std::string tee("hee");
	goto backward;
}
if (rand()%2 == 0)
	goto forward2;

{
	std::string tee("hee");
forward2:
	// tee isn't constructed here with goto. Crash if referenced I guess.
	// tee must be destructed at scope exit if entered without goto.
}

Does goto break all RAII objects?

Edited by Codarki, 16 November 2012 - 01:54 PM.


#47 SiCrane   Moderators   

11662
Like
0Likes
Like

Posted 16 November 2012 - 04:38 PM

A goto out of a block destroys all objects that have been initialized in that block. A goto into a block after a statement creating an object with an initializer is ill-formed (i.e. the compiler should bitch at you for trying it). So in your specific scenario, the first goto to forward should properly destroy tee. The goto to backward will destroy the second tee because control flow still leaves that block. The goto to forward2 should trigger a compiler error.




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.