Recursion problems in .NET

  • Thread starter Thread starter Ioannis Vranos
  • Start date Start date
I

Ioannis Vranos

The following code is supposed to print all folder names of a folder but it does not work.
Why?

Change the folder in main() to a folder suitable for your system so as to test it.


I created this proof-of-concept example, because I encountered similar recursion problems
in a more complex project.



#using <mscorlib.dll>

// Displays the full names of all contained folders and subfolders *recursively*
void DisplayFolderNames(System::String *folderName)
{
using namespace System;
using namespace System::IO;

DirectoryInfo * const dir= __gc new DirectoryInfo(folderName);

if(!dir->Exists)
return;

// Print contained folder names
DirectoryInfo *dirDirects[]= dir->GetDirectories();

if(dirDirects->Count== 0)
return;

for(long i=0; i<dirDirects->Count; ++i)
{
Console::WriteLine("{0}", dirDirects->FullName);

DisplayFolderNames(dirDirects->FullName);
}

}


int main()
{
DisplayFolderNames(S"D:\\extract\\temp");
}
 
Ioannis said:
The following code is supposed to print all folder names of a folder but
it does not work. Why?

Change the folder in main() to a folder suitable for your system so as
to test it.


I created this proof-of-concept example, because I encountered similar
recursion problems in a more complex project.



#using <mscorlib.dll>

// Displays the full names of all contained folders and subfolders
*recursively*
void DisplayFolderNames(System::String *folderName)
{
using namespace System;
using namespace System::IO;

DirectoryInfo * const dir= __gc new DirectoryInfo(folderName);

if(!dir->Exists)
return;

// Print contained folder names
DirectoryInfo *dirDirects[]= dir->GetDirectories();

if(dirDirects->Count== 0)
return;

for(long i=0; i<dirDirects->Count; ++i)
{
Console::WriteLine("{0}", dirDirects->FullName);

DisplayFolderNames(dirDirects->FullName);
}

}


int main()
{
DisplayFolderNames(S"D:\\extract\\temp");
}



My mistake, the code above works. However my "real one" still does not. I will try to
split it into threads though.


Can we avoid stack overflows by splitting recursion to multiple threads?
 
Ioannis said:
Can we avoid stack overflows by splitting recursion to multiple
threads?

In a few cases, yes, but it's probably not a good soluition.

The default stack size is 2mb. If you're recusing so deeply that you're
consuming 2mb of stack space, there's probably something wrong with your
design. You could just make the stack bigger (if you create a new thread),
but if you're consuming 3mb with today's test case, who's to say that you
won't need 4mb tomorrow? or 10mb or 100mb?

I really wouldn't expect the code you posted to have any problems with
exhausting the stack space - it should be consuming only a few (two dozen?)
bytes of stack per recursion.

-cd
 
Carl Daniel said:
In a few cases, yes, but it's probably not a good soluition.

The default stack size is 2mb. If you're recusing so deeply that you're
consuming 2mb of stack space, there's probably something wrong with your
design. You could just make the stack bigger (if you create a new
thread), but if you're consuming 3mb with today's test case, who's to say
that you won't need 4mb tomorrow? or 10mb or 100mb?

I really wouldn't expect the code you posted to have any problems with
exhausting the stack space - it should be consuming only a few (two
dozen?) bytes of stack per recursion.

-cd

Carl,

Sorry to correct you, but the default stack for thread/fibers in Win32 is
1MB, check the CreateThread API ins the SDK docs.

Willy.
 
Thank you all for the information, in the end the recursion problem was a bug in my code.
 
Willy said:
Carl,

Sorry to correct you, but the default stack for thread/fibers in
Win32 is 1MB, check the CreateThread API ins the SDK docs.

Yep. No need to be sorry - I just misremembered and didn't bother looking
it up.

-cd
 
Back
Top