Process returned -1073741571 (0xC00000FD) on my c++ code

As you can see in Window’s NT status reference, error code 0xC00000FD means stack overflow (usually caused by infinite recursion). In your case, it seems that you simply allocate a far too large array on the stack (line 57, baby babies[50000];), which is an array of size 50000*20=1000000. The simplest solution will be a dynamic allocation

baby* babies = new baby[50000];
// Your code here
delete[] babies;

A better solution would be to use std::vector which is a dynamic array that can grow and shrink. The simplest thing to do is to take a vector of size 50000, this way:

#include <vector>
...
std::vector<baby> babies(50000);

However, this is a poor solution as your pre-allocate 50000 elements even though you probably need much much less, and a better solution would be to add an element on-demand, using .push_back(element) method, or in your case, allocate n elements to the vector (impossible in a stack-allocated array).

I added your code with some modifications of mine:

#include <vector>
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;
ifstream fin("input.txt");

struct baby
{
    string gender;
    string name;
    int cnt = 0;
};

bool cmp(const baby& a, const baby& b)
{
    if (a.cnt > b.cnt) {
        return true;
    }
    return a.cnt == b.cnt && a.name < b.name;
}

bool are_equal(const baby& lhs, const baby& rhs)
{
    return lhs.gender == rhs.gender && lhs.name == rhs.name;
}

int howmany(const std::vector<baby>& babies, int i)
{

    int cnt = 0;
    for (int j = 0; j < babies.size(); j++)
    {
        if (babies[i].name == babies[j].name && babies[i].gender == babies[j].gender)
        {
            cnt++;
        }
    }
    return cnt;
}

void getData(std::vector<baby>& babies)
{
    for (int i = 0; i < babies.size(); i++)
    {
        fin >> babies[i].gender >> babies[i].name;
    }
}

int removeDuplicates(std::vector<baby>& babies)
{
    int j = 0;
    for (int i = 0; i < babies.size() - 1; i++)
    {
        if (babies[i].name != babies[i + 1].name) {
            babies[j++] = babies[i];
        }
    }
    babies[j++] = babies.back();
    return j;
}

void remove_duplicates_improved(std::vector<baby>& babies)
{
    babies.erase(babies.begin(), std::unique(babies.begin(), babies.end(), are_equal));
}

int main()
{
    int n;
    int top;
    fin >> n >> top;
    std::vector<baby> babies(n);
    getData(babies);
    for (int i = 0; i < n; i++)
    {
        babies[i].cnt = howmany(babies, i);
    }
    sort(babies.begin(), babies.begin() + n, cmp);

    remove_duplicates_improved(babies);
    int cnt = 0;
    for (int i = 0; i < babies.size(); i++)
    {
        if (cnt < top)
        {
            if (babies[i].gender == "F")
            {
                cout << babies[i].name << " ";
                cnt++;
            }
        }
    }
    cout << endl;
    cnt = 0;
    for (int i = 0; i < babies.size(); i++)
    {
        if (cnt < top)
        {
            if (babies[i].gender == "M")
            {
                cout << babies[i].name << " ";
                cnt++;
            }
        }
    }
    return 0;
}

Good luck

Leave a Comment