Stack overflow with no recursion?

1

As far as I can see, my code has no recursion, but I am getting exception 0xC00000FD. According to Rider For Unreal Engine, it is happening in the main function. It's being encountered at the decompiled code

mov byte ptr [r11], 0x0

It was working fine, then suddenly when I ran it a second time, just to make sure it worked, it broke. It now gives that exception every time. Here is my code:

// Language.cpp
#include <fstream>
#include <iostream>
#include <regex>
#include <stdexcept>
#include <string>


#include "Lexer/Lexer.h"
#include "Util/StrUtil.h"

int main(int argc, char* argv[])
{
    std::string line, fileString;
    std::ifstream file;
    file.open(argv[1]);
    
    if(file.is_open())
    {
        int lines = 0;
        
        while(file.good())
        {
            if (lines > 10000)
            {
                std::cerr << "Whoa there, that file's a little long! Try compiling something less than 10,000 lines." << '\n';
                return -1;
            }
            getline(file, line);
            fileString += line + (char)10;
            lines++;
        }
    }

    fileString += (char)0x00;

    std::string arr[10000];
    int arrLength = StrUtil::split(fileString, "\n", arr);

    Line lines[10000];
    Lexer::lex(arr, arrLength, lines);
    
    return 0;
}
// Lexer.cpp
#include "Lexer.h"

void Lexer::lex(std::string (&str)[10000], int length, Line (&lines)[10000])
{
    for (int i = 0; i < length; i++)
    {
        
    }
}
// StrUtil.cpp
#include "StrUtil.h"


#include <stdexcept>
#include <string>

int StrUtil::split(std::string str, std::string delimiter, std::string (&out)[10000])
{
    int pos = 0;
    out[0] = str;
    while (out[pos].find(delimiter) != std::string::npos)
    {
        const size_t found = out[pos].find(delimiter);
        out[pos + 1] = out[pos].substr(found + delimiter.length());
        out[pos] = out[pos].substr(0, found);
        pos++;
    }

    return pos + 2;
}

For custom types, Line is an array of Tokens, which are just <TokenType, std::string> pairs.

c++
lexer
asked on Stack Overflow Dec 16, 2020 by Lysander Mealy

1 Answer

2

As far as I can see, my code has no recursion

Indeed, there is no recursion in the shown code. Lack of recursion doesn't mean that you won't overflow the stack.

Stack overflow with no recursion?

Yes, this program is likely going to overflow the stack on some systems.

std::string arr[10000];

Typical size of std::string is 32 bytes (can vary greatly between language implementations). 10000 strings is 312 kiB. The execution stack on most desktop systems is one to few MiB. That one array is about the third of the memory that has to fit all of the automatic variables at the deepest stack frame of the program. It is very feasible that the remaning stack memory isn't enough for the rest of the program, especially considering you have another huge array of Line objects.

To fix the program, do not allocate massive variables such as this in automatic storage.

answered on Stack Overflow Dec 16, 2020 by eerorika

User contributions licensed under CC BY-SA 3.0