进程在我的 c++ 代码上返回 -1073741571 (0xC00000FD)

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

本文关键字:-1073741571 0xC00000FD 返回 我的 c++ 代码 进程      更新时间:2023-10-16

下面的 c++ 代码适用于某些输入,但它卡在测试 9(这里的输入数量为 6000(中,它给了我这条消息"进程返回 -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;
}

正如您在 Windows 的 NT 状态参考中看到的那样,错误代码0xC00000FD意味着堆栈溢出(通常由无限递归引起(。在您的情况下,似乎您只是在堆栈上分配了一个太大的数组(第 57 行,baby babies[50000];(,这是一个大小为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;
}

祝你好运