Why do does my program exit when I have my 2d array declared in main?

2

I am attempting to read from a text file and store all the words in a 2d array but for some reason my code only works when my 2d array is declared as a global variable (which is not allowed for this project). When I put my 2d array in main (as shown in the code example) I get an exit code "Process finished with exit code -1073741571 (0xC00000FD)". Any idea as to why this happening?

char theWord[maxLength]; // declare input space to be clearly larger than largest word

char dict_array[maxWords][maxLength];

while (inStream >> theWord) {
    
    for (int i = 0; i < strlen(theWord); i++) {

        dict_array[count][i] = tolower(theWord[i]);

    }

    if (strlen(theWord) >= 3) {
        count++;
    }
c++
arrays
asked on Stack Overflow Jul 11, 2020 by Nicholas

2 Answers

4

You've come to the right place to ask this, because 0xC00000FD means Stack Overflow!

Your code fails because char dict_array[maxWords][maxLength] is larger than the available stack space in your process. You can fix this easily by allocating the array using std::vector, operator new[], or malloc(), or you can fix it the hard way by increasing the size of your stack (how to do that depends on your OS, and like I said it's the hard way, don't bother).

The most idiomatic solution to this problem is std::vector<std::string>. Then you can allocate as much memory as your system has, and do so in a safe way with automatic deallocation (so-called RAII).

When it's a global variable it works fine because global variables are not allocated on the stack.

answered on Stack Overflow Jul 11, 2020 by John Zwinck
0

That error indicates a stack overflow. Your arrays are too large to live inside the function. Either make them global or, preferably, allocate them dynamically with new[]

answered on Stack Overflow Jul 11, 2020 by jkb

User contributions licensed under CC BY-SA 3.0