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

0

The c++ code below works fine for some inputs, but it is stuck at test 9 (number of inputs here is 6000) where it gives me this message "Process returned -1073741571 (0xC00000FD)".

This code reads information for n babies (their gender and name). Next it counts the appearances of each name then sorts the list of structures according to the appearances. Finally, it removes the duplicates and prints the top m female names and top m male names.

What does this error mean and what do I need to change to eliminate this error?

#include <iostream>
#include <fstream>
#include <algorithm>
#include <string.h>

using namespace std;
ifstream fin("input.txt");
struct baby
{
    string gender,name;
    int cnt;
};
bool cmp(baby a,baby b)
{
    if (a.cnt>b.cnt)
        return true;
    else if (a.cnt==b.cnt && a.name<b.name)
        return true;
    return false;
}
int howmany(baby babies[],int n,int i)
{

    int cnt=0;
    for (int j=0; j<n; j++)
    {
        if (babies[i].name==babies[j].name && babies[i].gender==babies[j].gender)
        {
            cnt++;
        }
    }
    return cnt;
}
void getData(baby babies[],int n)
{
    for (int i=0; i<n; i++)
    {
        fin>>babies[i].gender>>babies[i].name;

    }
}
int removeDuplicates(baby babies[],int n)
{
    int j=0;
    for (int i=0; i<n-1; i++)
    {
        if (babies[i].name!=babies[i+1].name)
            babies[j++]=babies[i];
    }
    babies[j++]=babies[n-1];
    return j; 
}
int main()
{
    int n,i,top,j;
    fin>>n>>top;
    baby babies[50000];
    getData(babies,n);  
    for (i=0; i<n; i++)
    {
        babies[i].cnt=howmany(babies,n,i); 
    }
    sort(babies,babies+n,cmp); 
    j=removeDuplicates(babies,n); 
    int cnt=0;
    for (int i=0; i<j; i++)
    {
        if (cnt<top)
        {
            if (babies[i].gender=="F")
            {
                cout<<babies[i].name<<" "; 
                cnt++;
            }
        }
    }
    cout<<endl;
    cnt=0;
    for (int i=0; i<j; i++)
    {
        if (cnt<top)
        {
            if (babies[i].gender=="M")
            {
                cout<<babies[i].name<<" "; 
                cnt++;
            }
        }
    }
    return 0;
}
c++
asked on Stack Overflow May 25, 2020 by UserL09 • edited May 25, 2020 by Richard Chambers

1 Answer

0

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

answered on Stack Overflow May 25, 2020 by Michael • edited May 25, 2020 by Michael

User contributions licensed under CC BY-SA 3.0