首页 > 解决方案 > 进程在我的 c++ 代码上返回 -1073741571 (0xC00000FD)

问题描述

下面的 c++ 代码适用于某些输入,但它卡在测试 9(这里的输入数为 6000),它给了我这条消息"Process returned -1073741571 (0xC00000FD)"

此代码读取n婴儿的信息(他们的性别和姓名)。接下来,它计算每个名称的出现次数,然后根据出现次数对结构列表进行排序。最后,它会删除重复项并打印出最热门的m女性名字和最热门的m男性名字。

这个错误是什么意思,我需要改变什么来消除这个错误?

#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++

解决方案


正如您在 Window 的NT 状态参考中看到的,错误代码 0xC00000FD 表示堆栈溢出(通常由无限递归引起)。在您的情况下,您似乎只是在堆栈上分配了一个太大的数组(第 57 行baby babies[50000];),这是一个 size 的数组50000*20=1000000。最简单的解决方案是动态分配

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

更好的解决方案是使用std::vector可以增长和缩小的动态数组。最简单的做法是采用大小为 50000 的向量,这样:

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

但是,这是一个糟糕的解决方案,因为您预先分配了 50000 个元素,即使您可能需要更少的元素,更好的解决方案是按需添加元素,使用.push_back(element)方法,或者在您的情况下,将n元素分配给向量(在堆栈分配的数组中是不可能的)。

我添加了您的代码和我的一些修改:

#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;
}

祝你好运


推荐阅读