-2

I've faced the stack overflow problem. As far as I got, the problem is in the size of a string array.

If I put 10^4 size it works fine, but if the size is increased to 10^5, it throws an error.


using namespace std;
 
 string getRow(int dig) { 
    string row = "";
    if (dig) {
        row += getRow((dig - 1) / 26);
        char c = 'A' + (dig - 1) % 26;
        row += c;
    }
    return row;
}

 
int main()
{
    int n,m;
    string inp[100000], col, row, last;
    smatch sm;
    auto pattern = regex("(R)(\\d+)(C)(\\d+)");
    auto pattern2 = regex("([A-Z]+)(\\d+)");
    
    cin >> n;
    
    for(int i=0; i < n; i++) 
        cin >> inp[i];
    
    for(int i=0; i < n; i++) {
        if(regex_match(inp[i], sm, pattern)) {
            
                
        }else if(regex_match(inp[i], sm, pattern2)) {
 
            
        }
        
    }
    
    return 0;
    
    
}
ikegami
  • 367,544
  • 15
  • 269
  • 518
S.Solomon
  • 5
  • 3
  • `string inp[100000]` On msvc which has a 1MB stack by default that would be a problem. Can't you use a `std::vector` instead? – drescherjm Oct 28 '22 at 14:55
  • 3221225725 is 0xC00000FD. That's a code Windows uses under exceptional circumstances. Well, 0xC0000005 is used by Windows in the event of a protection violation (SEGV), so I'm guessing 0xC00000FD is also from Windows. But I don't know under what exact circumstances it's used. – ikegami Oct 28 '22 at 14:56
  • IMHO, you should have the program return 8675309 instead. :-) – Thomas Matthews Oct 28 '22 at 14:57
  • @Thomas Matthews ? – ikegami Oct 28 '22 at 14:58
  • 1
    0xC00000FD is also STATUS_STACK_OVERFLOW :) – Wyck Oct 28 '22 at 14:59
  • If it really is a SO problem, I don't understand why you use recursion in getRow(). Get rid of that and all problems will be solved. – Sven Nilsson Oct 28 '22 at 14:59
  • You'd better throw away any book/website that teaches `std::string[100000]`. – Evg Oct 28 '22 at 15:01
  • 1
    Replace `string inp[100000]` with `std::vector inp(100000);` and your stack overflow will most likely be gone. You could also declare it after `cin >> n;` by making it `std::vector inp(n);` – Ted Lyngmo Oct 28 '22 at 15:01
  • @ikegami, Search the internet, for "Jessie's Girl". – Thomas Matthews Oct 28 '22 at 15:02
  • 3
    `string inp[100000]` -- Just because some constraint says "allow up to 100000 items" doesn't mean you should blindly declare an array of 100000 items. I usually see this sort of lazy hack from persons who are using "competitive coding" websites to learn C++. Then they usually post here as to why their program has a stack overflow or similar error. Instead of this, use `std::vector`, which grows dynamically depending on the amount of data. – PaulMcKenzie Oct 28 '22 at 15:02

1 Answers1

4

3221225725, or C00000FD in hex, is a code Windows uses when a stack overflow occurs. string inp[100000] creates 100000 strings on the stack, which is too large.

Here's how to program this correctly

vector<string> inp;
int n;    
cin >> n;

inp.reserve(n); // reserve space for the strings
for(int i=0; i < n; i++)
{
    string s;
    cin >> s;
    inp.push_back(s); // add one string
}

No limit on the number of strings. No stack overflow.

Declaring a zero-sized vector, reserving the correct size when it is known, and then adding items with push_back is the canonical way to handle a variable number of input items.

john
  • 85,011
  • 4
  • 57
  • 81