Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Are you a shit coder if you use goto?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Computer Talk
View previous topic :: View next topic  
Author Message
Black Magic
I post too much
Reputation: 5

Joined: 22 May 2007
Posts: 2221

PostPosted: Mon Nov 25, 2013 5:51 pm    Post subject: Are you a shit coder if you use goto? Reply with quote

teacher says yes, confirning with yall
Back to top
View user's profile Send private message Visit poster's website
Gniarf
Grandmaster Cheater Supreme
Reputation: 43

Joined: 12 Mar 2012
Posts: 1285

PostPosted: Mon Nov 25, 2013 6:20 pm    Post subject: Reply with quote

My teachers said the same, but Microsoft uses a lot of gotos in their driver examples. I would have loved to see their face if I said "Hey, download Microsoft Driver Development Kit and look at the included samples: there are 2300+ gotos there!"... Too bad I noticed that only after leaving school.

But still, in C/C++, you should avoid the gotos as much as possible, a lot of them going up and down tend to make your source harder to read.

_________________
DO NOT PM me if you want help on making/fixing/using a hack.
Back to top
View user's profile Send private message
justa_dude
Grandmaster Cheater
Reputation: 23

Joined: 29 Jun 2010
Posts: 891

PostPosted: Mon Nov 25, 2013 9:08 pm    Post subject: Reply with quote

You probably won't encounter instructors that are really skilled and knowledgeable about programming (or math or pretty much anything other than education itself) until university - everything else is painted in broad generalizations. In this case, they're probably just parroting the title of the famous "goto considered harmful" paper without ever having read it. The truth is that goto can be fabulously useful in C/C++. For example, you can use it to kind of simulate tail recursion - something that AFAIK isn't possible to do in any other way. It's, of course, possible to rewrite any recursive algorithm as iterative, but being able to do a more direct conversion while porting can be very helpful indeed.
Back to top
View user's profile Send private message
Gniarf
Grandmaster Cheater Supreme
Reputation: 43

Joined: 12 Mar 2012
Posts: 1285

PostPosted: Tue Nov 26, 2013 12:14 am    Post subject: Reply with quote

justa_dude wrote:
In this case, they're probably just parroting the title of the famous "goto considered harmful" paper without ever having read it.
Oops Embarassed, didn't even know it exited, so thanks, erm...Dude?
I might aswell include a link to this paper.

So if I understand what he says, it's basically "If you stop a process at some (break)point, with gotos it's harder to know where you're coming from". Not wrong, but we reverse engineer are pretty used to that, though he probably wants to keep regular programming easier than RE.

justa_dude wrote:
The truth is that goto can be fabulously useful in C/C++. For example, you can use it to kind of simulate tail recursion - something that AFAIK isn't possible to do in any other way.
I'm not familiar with tail recursion, but am I wrong if I say that this function tail recursive:
Code:
Type1 Foo(Type1 Parameter1)
{
  DoSomeProcessing(Parameter1);
  if (SomeCondition(Parameter1))
  {
    return Parameter1;
  }
  else
  {
    return Foo(Bar(Parameter1));
  }
}
?
_________________
DO NOT PM me if you want help on making/fixing/using a hack.
Back to top
View user's profile Send private message
justa_dude
Grandmaster Cheater
Reputation: 23

Joined: 29 Jun 2010
Posts: 891

PostPosted: Tue Nov 26, 2013 4:14 am    Post subject: Reply with quote

Gniarf wrote:
I'm not familiar with tail recursion, but am I wrong if I say that this function tail recursive:
Code:
Type1 Foo(Type1 Parameter1)
{
  DoSomeProcessing(Parameter1);
  if (SomeCondition(Parameter1))
  {
    return Parameter1;
  }
  else
  {
    return Foo(Bar(Parameter1));
  }
}
?


I'm not expert in compilers and programming languages either, but I'd say it has the form of tail recursion (the recursive call is the last statement to execute in the function). However, it will potentially crash if it the recursion is deep enough. Many languages (probably all functional languages) guarantee that such an arrangement will be optimized such that it can run forever without crashing the stack, but as far as I know C and C++ are not among them. It would be interesting to compile your example and see whether or not the stack frame gets optimized away under various compilers. But, anyway, the stack is relatively small and the number of parameters that can be passed via registers is quite limited, so it's in theory easily possible to make a program that will crash the stack via recursion. If your code is already in a tail recursive form, like yours, then you can pretty easily replace the last call with a goto to guarantee that you don't crash out.
Back to top
View user's profile Send private message
SteveAndrew
Master Cheater
Reputation: 30

Joined: 02 Sep 2012
Posts: 323

PostPosted: Tue Nov 26, 2013 11:44 am    Post subject: Reply with quote

Well I'm not sure about that example, but justa_dude is right, you can crash if you get too deep into the stack in a recursive function! (At least in C++ I know this is true) but yes perhaps not in all instances.

As for using goto, I try to avoid it these days, and pro-actively code in a way to not use them.

However if you get stuck in a situation where you could just add a goto, or have to change things around a lot. Maybe go with the goto Very Happy As people have mentioned and I never had any issues when using it, there really isn't anything wrong with it.

So in short try to avoid using it, but if you really need to, or your really being lazy (lol) then go for it! Smile

Or goto it rather! Wink haha

_________________
Back to top
View user's profile Send private message
Gniarf
Grandmaster Cheater Supreme
Reputation: 43

Joined: 12 Mar 2012
Posts: 1285

PostPosted: Tue Nov 26, 2013 3:56 pm    Post subject: Reply with quote

@justa_dude: So I compiled the code below and looks like VS2008 does tail call optimization.
According to those posts it would seem gcc also does tail call optimization.

C source:
Code:
#include <stdio.h>
#define Type1 int

int RoundCount;


Type1 Bar(Type1 Parameter1)
{
   return Parameter1-1;
}

void DoSomeProcessing(Type1& Parameter1)
{
   RoundCount++;
   Parameter1=Bar(Parameter1);
}

bool SomeCondition(Type1 Parameter1)
{
   return Parameter1==0;
}

Type1 Foo(Type1 Parameter1)
{
   DoSomeProcessing(Parameter1);
   if (SomeCondition(Parameter1))
   {
      return Parameter1;
   }
   else
   {
      return Foo(Bar(Parameter1));
   }
}

void main ()
{
   RoundCount=0;
   Foo(0xDeadBeef);
   printf("did %d recursions\n",RoundCount);
}


corresponding asm code:
Code:
010A1000 > >MOV ECX,1 ; this is Foo, with inlined DoSomeProcessing, SomeCondition, and Bar
010A1005   >ADD DWORD PTR DS:[RoundCount],ECX
010A100B   >SUB EAX,ECX
010A100D   >JE SHORT TailRecu.010A101B
010A100F   >NOP
010A1010   >ADD DWORD PTR DS:[RoundCount],ECX
010A1016   >SUB EAX,2
010A1019  ^>JNZ SHORT TailRecu.010A1010  ; tail call optimization here
010A101B   >RETN
010A101C   >INT3
010A101D   >INT3
010A101E   >INT3
010A101F   >INT3
010A1020 > >MOV EAX,DEADBEED  ; my beef ! ;(
010A1025   >MOV DWORD PTR DS:[RoundCount],1
010A102F   >CALL TailRecu.Foo
010A1034   >MOV EAX,DWORD PTR DS:[RoundCount]
010A1039   >PUSH EAX
010A103A   >PUSH OFFSET TailRecu.??_C@_0BD@CL.... ; ASCII "did %d recursions"
010A103F   >CALL DWORD PTR DS:[<&MSVCR90.printf>]              ; MSVCR90.printf
010A1045   >ADD ESP,8
010A1048   >XOR EAX,EAX
010A104A   >RETN


Conclusion: you can do safe tail recursion without explicit goto in C if you have a modern compiler. Not sure it works ALL the time (it should), but this optimization is done at least sometimes.

_________________
DO NOT PM me if you want help on making/fixing/using a hack.
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 199

Joined: 25 Jan 2006
Posts: 8518
Location: 127.0.0.1

PostPosted: Tue Nov 26, 2013 4:05 pm    Post subject: Reply with quote

Typically, if you need goto, you are most-likely doing something in a bad manner and it can be coded better. At most, goto is mainly useful for breaking out of embedded loops. Even then you can avoid it using variables, but ultimately it isn't required.

However, if you compile down a goto and look at the asm, it is just a jump. There isn't really anything wrong with it when you view what it compiles down to, but most programmers look down on it as either 'lazy-coding' or 'bad coding'.

Goto isn't needed for recursion at all either.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
justa_dude
Grandmaster Cheater
Reputation: 23

Joined: 29 Jun 2010
Posts: 891

PostPosted: Tue Nov 26, 2013 8:15 pm    Post subject: Reply with quote

Gniarf wrote:
looks like VS2008 does tail call optimization. it would seem gcc also does tail call optimization.


Hey, good job mate. I kind of suspected that would be the case, but thanks for doing the hard yards. Note, however, that I chose my wording pretty carefully: "Many languages (probably all functional languages) guarantee that such an arrangement will be optimized such that it can run forever without crashing the stack." So, it's nice that in this case it got optimized away, but you can't really count on it being done.

Wiccan wrote:
Goto isn't needed for recursion at all either.

No, it isn't. Computers aren't recursive in nature, so it's trivially easy to prove that any recursive program can be ported to an iterative one. That said, the tail recursion idiom is VERY common in a lot of functional programming languages and it has been my personal experience that the fastest and most correct way to port them is generally with a goto rather than a helper function or other convoluted logic. Is it absolutely NEEDED? No, but neither are functions, classes, structures or most of the syntactic sugar otherwise commonly used.
Back to top
View user's profile Send private message
Gniarf
Grandmaster Cheater Supreme
Reputation: 43

Joined: 12 Mar 2012
Posts: 1285

PostPosted: Tue Nov 26, 2013 10:26 pm    Post subject: Reply with quote

justa_dude wrote:
Note, however, that I chose my wording pretty carefully: "Many languages (probably all functional languages) guarantee that such an arrangement will be optimized such that it can run forever without crashing the stack." So, it's nice that in this case it got optimized away, but you can't really count on it being done.
Noted. And I can guarantee you that VS2008 does NOT optimize away tail recursion by default in debug mode, so debugging such functions without a goto might be chore. There might be an option to enable enable tail call optimization alone in debug mode, but I can't access my project properties atm 'coz dotnet is on the fritz AGAIN, as usual after a fresh os install. Gotta find which dotnetsetup to run and how to make my os chew it, but that's off-topic...

EDIT: got it to work, was easier the previous times. So I didn't find any compiler optimization that was specially geared toward recursive function: that doesn't bode well for goto-less tail-recursive function debugging. Maybe using telling the compiler to do optimize a specific function only could do the trick.

_________________
DO NOT PM me if you want help on making/fixing/using a hack.


Last edited by Gniarf on Tue Nov 26, 2013 11:40 pm; edited 1 time in total
Back to top
View user's profile Send private message
justa_dude
Grandmaster Cheater
Reputation: 23

Joined: 29 Jun 2010
Posts: 891

PostPosted: Tue Nov 26, 2013 11:36 pm    Post subject: Reply with quote

Gniarf wrote:
justa_dude wrote:
Note, however, that I chose my wording pretty carefully: "Many languages (probably all functional languages) guarantee that such an arrangement will be optimized such that it can run forever without crashing the stack." So, it's nice that in this case it got optimized away, but you can't really count on it being done.
Noted. And I can guarantee you that VS2008 does NOT optimize away tail recursion by default in debug mode, so debugging such functions without a goto might be chore.


It's worse than being difficult to debug, though. It means that if the recursion is sufficiently deep that you can overflow the stack and crash. So, code that is correct in -O2 or whatever optimization could become unstable with other compiler settings or compilers or whatever.

All that aside, I maintain that goto is a tool that can be used properly or improperly. Coders will often tell you that your code is bad or poor for stupid reasons - you'd never believe how much time is wasted on in-department fighting over stylistic stuff (like how many spaces in an indent and whether or not to use tabs). Having a goto in your code does not automatically make it bad. Q: Are you a shit coder if you use goto? A: No, but you're a shit coder if you can't structure a program well enough to run without it (which isn't the same as saying you should).
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Computer Talk All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites