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 


Optimisation challenges
Goto page Previous  1, 2, 3
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Wed Jun 16, 2010 10:43 am    Post subject: Reply with quote

Burningmace wrote:
I have always been taught from square one that recursion is a difficult beast, and that it is best to be avoided except where absolutely necessary.

I hope this has not happened from a formal teaching of programming. If that is the case, I really question to competence of your teacher. Just because something is hard it should not be done ? Btw, you may not have come across it but in some languages, recursion is almost part of that particular paradigm. For example, functional programming languages such as Haskell or Q make it such that iterative programming is actually a lot harder than recursive programming.
Burningmace wrote:
The maximum stack size is not a single defined value, and thus is unpredictable. In an open source scenario, one would expect to have the code compiled on multiple architectures with multiple compilers. Whilst GCC might default to 8MB of stack space, another compiler might choose 1MB or 2MB.

It can be set in linker options.
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8588
Location: 127.0.0.1

PostPosted: Wed Jun 16, 2010 11:32 am    Post subject: Reply with quote

Question for you Burningmace, is this topic created with the intent of actually learning or because you are doing a challenge on a site such as HackQuest and are looking for help with one of their challenges? I'm asking since this exact topic is one of their challenges, so looked familiar.
_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Polynomial
Grandmaster Cheater
Reputation: 5

Joined: 17 Feb 2008
Posts: 524
Location: Inside the Intel CET shadow stack

PostPosted: Thu Jun 17, 2010 12:07 pm    Post subject: Reply with quote

Slugsnack, actually it has been two teachers and multiple online learning references that have steered me away from recursion. I guess it's something that people avoid due to the somewhat difficult and troublesome nature of it. I know that Haskell uses it in a lot of situations, but I remember reading somewhere that the stack in Haskell is automatically expanded if it gets near the limit.

Wiccaan, it is just a learning topic. I've never heard of HackQuest, it looks interesting. I shall take a look when I have time.

_________________
It's not fun unless every exploit mitigation is enabled.
Please do not reply to my posts with LLM-generated slop; I consider it to be an insult to my time.
Back to top
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Thu Jun 17, 2010 12:20 pm    Post subject: Reply with quote

Burningmace wrote:
Slugsnack, actually it has been two teachers and multiple online learning references that have steered me away from recursion. I guess it's something that people avoid due to the somewhat difficult and troublesome nature of it. I know that Haskell uses it in a lot of situations, but I remember reading somewhere that the stack in Haskell is automatically expanded if it gets near the limit.


Recurrsion is bad, mmkay? Rolling Eyes

Code:
open System
open System.IO;
open System.Text.RegularExpressions;

let rec FilesInDirectoryMatching (regex : Regex) (dir : DirectoryInfo) =
    [ yield! dir.GetFiles() |> Array.Parallel.choose (fun x -> if regex.IsMatch x.Name then Some x else None)
      for subdir in dir.GetDirectories() do yield! FilesInDirectoryMatching regex subdir ]

let reg = Regex(".*")
let dir = DirectoryInfo(@"D:\Downloads")

FilesInDirectoryMatching reg dir |> List.map (fun x -> x.Name) |>  printfn "%A"
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Thu Jun 17, 2010 3:43 pm    Post subject: Reply with quote

Burningmace wrote:
but I remember reading somewhere that the stack in Haskell is automatically expanded if it gets near the limit..

Slugsnack wrote:
A little extra info for interest. The stack is simply a block of memory that is defined by the program for specific use, ie. as the stack. Its main use is parameter passing and local variables. Now the interesting bit. The stack is actually extendable and can grow. If on a heavily recursive app, the stack commit size is exceeded, the next probe triggers a guard-page exception which causes the stack to extend and the guard page to increment to the next page.
Back to top
View user's profile Send private message
Polynomial
Grandmaster Cheater
Reputation: 5

Joined: 17 Feb 2008
Posts: 524
Location: Inside the Intel CET shadow stack

PostPosted: Thu Jun 17, 2010 5:11 pm    Post subject: Reply with quote

Slugsnack - That may well have been where I read it. I've been rather tired recently (download festival, woooo!) so I forget things easily Wink Does this stack probe mechanism hold for all languages? I remember back in the days of VB6 it was annoyingly easy to cause a stack overflow with recursive code - I forget the exact number of calls that killed it but it wasn't a lot, in the order of a few thousand I think. In fact, VB6 tutorials were where I got a lot of my "recursion is bad" lectures from.

Flyte - Useful and recursive in high level terms, but does it result in recursive or iterative code at assembly level? A friend asserted to me that languages with inherent "recursivity" are often compiled into an iterative form. He went on to state that some compilers even give each expression its own "virtual stack" which eliminates the problems of using the standard stack with call/ret. Regardless, this thread is a catalog of why recursion can be good. I'll regard it in higher terms in future.

_________________
It's not fun unless every exploit mitigation is enabled.
Please do not reply to my posts with LLM-generated slop; I consider it to be an insult to my time.
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Thu Jun 17, 2010 6:14 pm    Post subject: Reply with quote

The stack being extended does not mean it can't overflow. Just means there is a large space that is reserved but it is only committed to a certain size which can then keep growing up to the reserved size.
Back to top
View user's profile Send private message
Polynomial
Grandmaster Cheater
Reputation: 5

Joined: 17 Feb 2008
Posts: 524
Location: Inside the Intel CET shadow stack

PostPosted: Thu Jun 17, 2010 8:14 pm    Post subject: Reply with quote

From the way I read it (it may not have been your post) it suggested that the stack would expand as much as possible, to the limit of as much memory that can be allocated by a single process. Think of it as the Stack class in the .NET framework, where the whole stack is simply an object in virtual memory and not the traditional stack pointed to by the ESP register. Each entry added allocates more memory automatically.
_________________
It's not fun unless every exploit mitigation is enabled.
Please do not reply to my posts with LLM-generated slop; I consider it to be an insult to my time.
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Thu Jun 17, 2010 8:26 pm    Post subject: Reply with quote

The stack must be all in contiguous memory and it can not simply be moved around to reallocate it like with realloc() or something because it would involve having to update ESP + ESP + PE headers, etc.

Each thread has a block of memory reserved for the stack. Only a proportion of that memory is committed and then the guard page will expand the committed memory. I believe the reason it's implemented like this is so you don't end up wasting tonnes of memory on unused stack memory.
Back to top
View user's profile Send private message
Polynomial
Grandmaster Cheater
Reputation: 5

Joined: 17 Feb 2008
Posts: 524
Location: Inside the Intel CET shadow stack

PostPosted: Fri Jun 18, 2010 7:22 am    Post subject: Reply with quote

I was talking more about a 'virtualised' system, where instructions would be translated into low level code but the higher level program stack kept in more managed conditions. I imagine this is how interpreted script languages such as PHP work.

For example, I could write code in C# that parsed a set of imitation low level instructions (including call/ret/push/pop equivilents) and modified the state of a large byte array (virtual memory) and a stack object (the virtual stack) as normal programs would do. This would create similar results to that of a native executable program (albeit with a significant performance hit) yet never directly access the real stack - the whole thing would remain managed. The managed stack in CIL is similar to this, where all CIL code is executed by the virtual machine.

But all of this is off topic. Challenges please!

_________________
It's not fun unless every exploit mitigation is enabled.
Please do not reply to my posts with LLM-generated slop; I consider it to be an insult to my time.
Back to top
View user's profile Send private message
Deltron Z
Expert Cheater
Reputation: 1

Joined: 14 Jun 2009
Posts: 164

PostPosted: Sun Jun 20, 2010 2:58 am    Post subject: Reply with quote

We've already solved your challanges.
I solved the first 3 in C/C++ and Flyte gave another solution in what seems like C#, is this J#? looks cool, I didn't even know they still use it...
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Sun Jun 20, 2010 6:09 am    Post subject: Reply with quote

F#
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Goto page Previous  1, 2, 3
Page 3 of 3

 
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 can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites