Recursion and Stack Overflow(C++)

Status
Not open for further replies.

AD

Ars Praetorian
519
code:<HR>
Code:
class Cstr {
public:
	Cstr* pNext;
	CString FileStr;
	Cstr()
	{ 
		pNext = NULL;
	}
};

Cstr Head;

void addStr(CString Add)
{
	Cstr* pCurrent = &Head;
	if (Head.FileStr == "")
	{
		Head.FileStr = Add;
	}
	else
	{
	while (pCurrent->pNext)
	{
		pCurrent = pCurrent->pNext;
	}
	Cstr* temp = pCurrent;
	pCurrent->pNext = new Cstr;
	pCurrent = pCurrent->pNext;
	pCurrent->FileStr = Add;
	temp->pNext = pCurrent;
	}
}

bool isAdded(CString out)
{
	bool found = false;
	Cstr* pCurrent;
	pCurrent = &Head;
	while (pCurrent->pNext != 0 && found == false)
	{
		if (out.CompareNoCase(pCurrent->FileStr) == 0)
		{
			found = true;
		}
		pCurrent= pCurrent->pNext;
	}
	return found;
}

		
void KillLL()
{
	Cstr* Corpse;
	Cstr* pCurrent = &Head;
	pCurrent = pCurrent->pNext;
	while(pCurrent->pNext)
	{
		Corpse = pCurrent;
		pCurrent = pCurrent->pNext;
		delete Corpse;	
	}
	delete pCurrent;
}

		
		
void FindFilesR(CString SearchSpec)
{	
	CFileFind Finder;
	Finder.FindFile(SearchSpec);	
	while(Finder.FindNextFile())
	{
		if (Finder.IsDots()) 
		{ 
			continue;
		}
		if (Finder.IsDirectory())
		{ 
			CString Path;
			Path = Finder.GetFilePath();
			Path += "\\*.*";
			if(!isAdded((LPCTSTR)Finder.GetFileTitle()))
			{
				cout << (LPCTSTR) Finder.GetFileTitle() << "\n";
				addStr((LPCTSTR)Finder.GetFileTitle());
			}
			FindFilesR(Path);
			continue;
		}
		if(!isAdded((LPCTSTR)Finder.GetFileTitle()))
		{
			cout << (LPCTSTR) Finder.GetFileTitle() << "\n";
			addStr((LPCTSTR)Finder.GetFileTitle());
		}
	}
	if (!Finder.IsDots()) 
	{ 
		if (Finder.IsDirectory())
		{ 
			CString Path;
			Path = Finder.GetFilePath();
			Path += "\\*.*";
			if(!isAdded((LPCTSTR)Finder.GetFileTitle()))
			{
				cout << (LPCTSTR) Finder.GetFileTitle() << "\n";
				addStr((LPCTSTR)Finder.GetFileTitle());
			}
			FindFilesR(Path);
		}
		if(!isAdded((LPCTSTR)Finder.GetFileTitle()))
		{
			cout << (LPCTSTR) Finder.GetFileTitle() << "\n";
			addStr((LPCTSTR)Finder.GetFileTitle());
		}
	}
}


If I give it a directory with more than 11,000 files/folders within it and its subfolders I get a stack overflow. Any ideas?
 
Do a nonrecursive top-down tree-walk:

list = empty;
entry->Dir = StartingDirectory;

AddToTail(list, entry);

do {

entry = PopHeadElement(list);
currentDir = entry->Dir;

for(each entry in currentDir)
{
// If file, examine file
// If directory, create new entry, AddToTail(...);
}
} until(!list->IsEmpty());

- Not shown, memory management, . & .. culling, etc.

Also, be very careful if you are going to deploy something like this into a secure environment. Sometimes a directory isn't a directory, rather it's a hard-link or mount-point to another drive or directory.

An evil user could create a hard-link to try to trick you into deleting or examining something they couldn't. Windows has APIs that can tell the difference (Unix's probably do too).
 

AD

Ars Praetorian
519
actually, my program would fail if it was asked to list a dir with 11,000 dirs below it (one dir in another, in another)

the problem was it was trying to access "System Volume Information" and it doesn't have permission, but the only way to fix that would be to test permissions before list them, but to test permissions i need the name :mad: Don't ya hate MS and their infinate wisdom? or is their a way around this?
 
Status
Not open for further replies.