Hi all, and thanks in advance for your help.

I have a program which stores some large arrays of ints and reads and writes from text files. main() takes commands with cin and then runs various functions. One of these, generateSentence(), takes three ints and a char* as arguments, but a stack overflow exception is thrown when the function is called. No large variables exist in main(), and all my big arrays are allocated dynamically. Other functions, such as absorb(), run without any problem. In desperation, I changed the stack size in properties, which was mysteriously at 0, to 1,000,000, but to no avail.

(Sorry if this is a simple issue - I'm an experienced programmer but new to C++ and Visual Studio.)

Here's the code for main():

int main ()
{
	int mode = 0;
	int seedOffset = 0;
	int files = 0;

	int maxDepth = 0;

	string command;
	string dictName;
	char* fileName = "None";
	char* dictionaryName = "None";
	char* sequenceFileName;
	string lastFileName = "None";

	sequenceFileName = new (nothrow) char [30];

	sequenceFileName[0] = '\0';

	while (1) {

		cout << "Command or file to absorb: ";
		cin >> command;
		
		if (command == "q")
			return 1;

		else if (command == "c") {
			cout << "Links cleared." << endl;
			clearLinks();
		}

		else if (command == "g") {
			if (dictionaryName == "None") {
				cout << endl << "Dictionary file? ";
				cin >> dictName;
				dictionaryName = &dictName[0];
			}

			generateSentence(mode, seedOffset, dictionaryName, defMaxDepth);
			seedOffset++;
			cout << endl;
		}

		else if (command == "gdict") {
			cout << endl << "Dictionary file? ";
			cin >> dictName;
			dictionaryName = &dictName[0];
			generateSentence(mode, seedOffset, dictionaryName, defMaxDepth);
			seedOffset++;
			cout << endl;
		}
		else if (command == "gdepth") {
			if (dictionaryName == "None") {
				cout << endl << "Dictionary file? ";
				cin >> dictName;
				dictionaryName = &dictName[0];
			}
			cout << endl << "Maximum link depth? ";
			cin >> maxDepth;
			generateSentence(mode, seedOffset, dictionaryName, maxDepth + 1);
			seedOffset++;
			cout << endl;
		}

		else if (command == "gs") {
			if (dictionaryName == "None") {
				cout << endl << "Dictionary file? ";
				cin >> dictName;
				dictionaryName = &dictName[0];
			}
			for (int i = 0; i < 12; i++) {
				generateSentence(mode, seedOffset, dictionaryName, defMaxDepth);
				seedOffset++;
			}
			cout << endl;
		}

		else if (command == "d") {
			mode = DISPLAY;
			cout << "Display mode:" << endl;
		}

		else if (command == "n") {
			mode = NORMAL;
			cout << "Normal mode:" << endl;
		}

		else if (command == "adepth") {
			cout << endl << "File to absorb? ";
			cin >> command;
			fileName = &command[0];
			cout << endl << "Dictionary file? ";
			cin >> dictName;
			dictionaryName = &dictName[0];
			cout << endl << "Maximum link depth? ";
			cin >> maxDepth;
			if (dictName == "new") {
				FILE* outFile;
				fopen_s(&outFile, "dictionary.txt", "w");
				fclose(outFile);
				dictionaryName = "dictionary.txt";
			}
			cout << endl << "Absorbing " << fileName << " ..." << endl;
			absorb(fileName, dictionaryName, mode, maxDepth + 1);
		}

		else if (command == "asequence") {
			cout << endl << "Base file name to absorb? ";
			cin >> command;
			fileName = &command[0];
			cout << endl << "Dictionary file? ";
			cin >> dictName;
			dictionaryName = &dictName[0];
			cout << endl << "Maximum link depth? ";
			cin >> maxDepth;
			cout << endl << "Files in sequence? ";
			cin >> files;
			if (dictName == "new") {
				FILE* outFile;
				fopen_s(&outFile, "dictionary.txt", "w");
				fclose(outFile);
				dictionaryName = "dictionary.txt";
			}
			for (int i = 1; i <= files; i++) {
				sequenceFileName[0] = '\0';
				strcat(sequenceFileName, fileName);
				char num = i + '0';
				char numbox[2];
				numbox[0] = num;
				numbox[1] = '\0';
				strcat(sequenceFileName, numbox);
				cout << endl << "Absorbing " << sequenceFileName << " ..." << endl;
				absorb(sequenceFileName, dictionaryName, mode, maxDepth + 1);
			}
		}

		else {
			if (command == "s")
				fileName = &lastFileName[0];
			else fileName = &command[0];

			lastFileName = fileName;
			cout << endl << "Dictionary file? ";
			cin >> dictName;
			dictionaryName = &dictName[0];
			if (dictName == "new") {
				FILE* outFile;
				fopen_s(&outFile, "dictionary.txt", "w");
				fclose(outFile);
				dictionaryName = "dictionary.txt";
			}
			cout << endl << "Absorbing " << fileName << " ..." << endl;
			absorb(fileName, dictionaryName, mode, defMaxDepth);
		}

		
	}
	return 1;
}

The full file is attached.

Thanks for any and all help!

what version of vs are you using? I tried to compile that *.cpp file using VC++ 2008 Express and get lots of errors. There have been lots of changes between vc++ 6.0 and 2008, so you might want to compile it with 2008.

Also, please post randomc.h

commented: Thanks for the help! +0

Here are randomc.h and mersenne.cpp, which are the only other code files that syntactica.cpp uses. I am using Visual C++ 2008 Express, and I don't get any errors when I compile. Could you tell me some of the errors you're having?

Thanks so much for your help.

what version of vs are you using? I tried to compile that *.cpp file using VC++ 2008 Express and get lots of errors. There have been lots of changes between vc++ 6.0 and 2008, so you might want to compile it with 2008.

Also, please post randomc.h

There are lots of these

1>c:\dvlp\test10\test10\test10.cpp(659) : warning C4258: 'i' : definition from the for loop is ignored; the definition from the enclosing scope is used
1> c:\dvlp\test10\test10\utils.cpp(646) : definition of 'i' ignored

Although they are warnings you need to correct the problems because they are actual errors. It is trying to tell you what you have attempted to use the value of a loop counter that was declared inside a for loop, which is not legal in c++.

for(int i = 0; <snipped> )

if( i == something) // illegal because i was declared above

Cool, I can do that. Are these related to the stack overflow I'm getting, or is that a separate issue?

There are lots of these


Although they are warnings you need to correct the problems because they are actual errors.

Don't know yet because I need a clean compile.

Here it is, fixed. I only get four warnings, and they're from converting types. Hope this helps!

Don't know yet because I need a clean compile.

>> int goodLinks[kWordL + 1][kLinksL];

That's the culprit that is causing stack overflow. Make it a pointer and allocate the memory with new.

You are also going to have lots of problems with concantinating ".txt" to the pointers such as filename cause those pointers were never allocated enough space to hold the additional text. Suggest you replace all those pointers with std::string to avoid those problems. For example

std::string fileName = "None";
	std::string dictionaryName = "None";
	std::string sequenceFileName;
	string lastFileName = "None";

When you do that you will also have to change the function parameters to use std::string. Well, you really don't HAVE to do that but it would make your program a lot safer. Other changes will be needed too.

Great, thanks a lot! Worked perfectly. I should have noticed that array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.