gpt4 book ai didi

c++ - 段错误 :- LIBC_FATAL_STDERR

转载 作者:行者123 更新时间:2023-11-30 05:22:55 25 4
gpt4 key购买 nike

我在程序结束前收到段错误。那是在执行完所有语句后,我得到了错误。

代码如下

#include<iostream>
#include <stdlib.h>
#include<vector>
#include<algorithm>
#include <queue>
using namespace std;

int bipartite(int G[1010][1010], int source, int n)
{
int color[n+1];
for(int i=1; i<n+1; i++)
color[i]=-1;

color[source]=1;
queue<int> q;
q.push(source);

int done=0;
while(done!=1)
{
while (!q.empty())
{
int u = q.front();
q.pop();
for (int v = 1; v < n+1; ++v)
{
if(u==v)
continue;
if (G[u][v] && color[v] == -1)
{
color[v] = 1 - color[u];
q.push(v);
//cout<<"V PUSH: "<<v<<u<<endl;
}

else if (G[u][v] && color[v] == color[u])
{
//cout<<"OUT: "<<v<<u<<endl;
return 0;
}
}
}
int i;
for(i=1; i<=n+1; i++)
{
if(color[i]==-1)
{
q.push(i);
continue;
}
}
if(i==n+2)
done=1;
}

return 1;
}

int main()
{
int t;
cin>>t;
int n,m,a,b;
int Array[1010][1010];
while(t--)
{
cin >> n >> m;
for(int i=0; i<=1010; i++)
for(int j=0; j<=1010; j++)
Array[i][j]=0;

for(int i=1; i<=m; i++)
{
cin >> a >> b;
Array[a][b] = 1;
Array[b][a] = 1;
}
for(int i=0; i<=1010; i++)
{
for(int j=0; j<=1010; j++)
{
if(Array[i][j]==1)
Array[i][j]=0;
else
Array[i][j]=1;
}
}
for(int i=0; i <=1010; i++)
Array[i][i]=0;

if (bipartite(Array, 1, n) == 1)
cout << "YES" << endl;
else
cout << "NO" << endl;

}

cout<<"A"<<endl;
return 0;
}

对于输入:-1个3 21 22 3

答案打印正确,但在我收到段错误之后。

The gdb error is Program received signal SIGSEGV, Segmentation fault.
__GI_getenv (name=0x7ffff76033c2 "BC_FATAL_STDERR_",
name@entry=0x7ffff76033c0 "LIBC_FATAL_STDERR_") at getenv.c:84
84 getenv.c: No such file or directory

最佳答案

你在这里超出了你的数组边界:

for(int i=0; i<=1010; i++)
for(int j=0; j<=1010; j++)
Array[i][j]=0;

因此,您会得到不可预知的行为。正确的条件是 <而不是 <= :

for(int i=0; i<1010; i++)
for(int j=0; j<1010; j++)
Array[i][j]=0;

其他上限检查也应修复。无论你在哪里检查它都是 <= 1010 , 但应该是 < 1010 .

在许多地方,您通过索引访问数组元素而不进行任何检查,这是一种危险的方法。

关于c++ - 段错误 :- LIBC_FATAL_STDERR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39430753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com