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!