Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive code exits with exit code 3221225477

Tags:

c++

recursion

I am new to c++ programming and StackOverflow, but I have some experience with core Java. I wanted to participate in programming Olympiads and I choose c++ because c++ codes are generally faster than that of an equivalent Java code.

I was solving some problems involving recursion and DP at zonal level and I came across this question called Sequence game

But unfortunately my code doesn't seem to work. It exits with exit code 3221225477, but I can't make anything out of it. I remember Java did a much better job of pointing out my mistakes, but here in c++ I don't have a clue of what's happening. Here's the code btw,

#include <iostream>
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
int N, minimum, maximum;
set <unsigned int> result;
vector <unsigned int> integers;
bool status = true;

void score(unsigned int b, unsigned int step)
{
    if(step < N)
    {
        unsigned int subtracted;
        unsigned int added = b + integers[step];
        bool add_gate = (added <= maximum);
        bool subtract_gate = (b <= integers[step]);
        if (subtract_gate) 
            subtracted = b - integers[step];
        subtract_gate = subtract_gate && (subtracted >= minimum);
        if(add_gate && subtract_gate)
        {
            result.insert(added);
            result.insert(subtracted);
            score(added, step++);
            score(subtracted, step++);
        }
        else if(!(add_gate) && !(subtract_gate))
        {
            status = false;
            return;
        }
        else if(add_gate)
        {
            result.insert(added);
            score(added, step++);
        }
        else if(subtract_gate)
        {
            result.insert(subtracted);
            score(subtracted, step++);
        }
    }
    else return;
}
int main()
{
    ios_base::sync_with_stdio(false);

    ifstream input("input.txt"); // attach to input file
    streambuf *cinbuf = cin.rdbuf(); // save old cin buffer
    cin.rdbuf(input.rdbuf()); // redirect cin to input.txt

    ofstream output("output.txt"); // attach to output file
    streambuf *coutbuf = cout.rdbuf(); // save old cout buffer
    cout.rdbuf(output.rdbuf()); // redirect cout to output.txt

    unsigned int b;
    cin>>N>>b>>minimum>>maximum;

    for(unsigned int i = 0; i < N; ++i)
        cin>>integers[i];
    score(b, 0);
    set<unsigned int>::iterator iter = result.begin();
    if(status)
        cout<<*iter<<endl;
    else 
        cout<<-1<<endl;

    cin.rdbuf(cinbuf);
    cout.rdbuf(coutbuf);

    return 0;
}

(Note: I intentionally did not use typedef).

I compiled this code with mingw-w64 in a windows machine and here is the Output:

[Finished in 19.8s with exit code 3221225477] ...

Although I have an intel i5-8600, it took so much time to compile, much of the time was taken by the antivirus to scan my exe file, and even sometimes it keeps on compiling for long without any intervention from the anti-virus.

(Note: I did not use command line, instead I used used sublime text to compile it). I even tried tdm-gcc, and again some other peculiar exit code came up. I even tried to run it on a Ubuntu machine, but unfortunately it couldn't find the output file. When I ran it on a Codechef Online IDE, even though it did not run properly, but the error message was less scarier than that of mingw's. It said that there was a run-time error and "SIGSEGV" was displayed as an error code. Codechef states that

A SIGSEGV is an error(signal) caused by an invalid memory reference or a segmentation fault. You are probably trying to access an array element out of bounds or trying to use too much memory. Some of the other causes of a segmentation fault are : Using uninitialized pointers, dereference of NULL pointers, accessing memory that the program doesn’t own.

It's been a few days that I am trying to solve this, and I am really frustrated by now. First when i started solving this problem I used c arrays, then changed to vectors and finally now to std::set, while hopping that it will solve the problem, but nothing worked. I tried a another dp problem, and again this was the case.

It would be great if someone help me figure out what's wrong in my code. Thanks in advance.

like image 457
NeelDigonto Avatar asked Sep 20 '26 23:09

NeelDigonto


1 Answers

3221225477 converted to hex is 0xC0000005, which stands for STATUS_ACCESS_VIOLATION, which means you tried to access (read, write or execute) invalid memory.

I remember Java did a much better job of pointing out my mistakes, but here in c++ I don't have a clue of what's happening.

When you run into your program crashing, you should run it under a debugger. Since you're running your code on Windows, I highly recommend Visual Studio 2017 Community Edition. If you ran your code under it, it would point exact line where the crash happens.

As for your crash itself, as PaulMcKenzie points out in the comment, you're indexing an empty vector, which makes std::cin write into out of bounds memory.

like image 139
Sunius Avatar answered Sep 23 '26 20:09

Sunius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!