我写了以下代码。它在某些输入上运行良好,但在其他输入上显示错误。
例如,当我尝试输入时:
10000 2
1 2
3 4
它在我输入第二行输入时显示以下错误(甚至没有等待第三行)
a.out: malloc.c:2395: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
Aborted (core dumped)
可能是什么原因?
#include <iostream>
#include <list>
#include <cmath>
using namespace std;
void addEdge(list<int> *adj, int a, int b)
{
adj[a].push_back(b);
}
int count = 1;
int dfsVisit(int i, bool *visited,list<int> *adj)
{
visited[i] = true;
list<int> ::iterator k;
for(k=adj[i].begin(); k!=adj[i].end();k++)
{
if(!visited[*k])
{
count ++;
dfsVisit(*k, visited, adj);
}
}
return count;
}
int main()
{
list<int> *adj;
long int n,i,j,a,b,m=0,k;
long int arr[1000000];
cin>>n>>i;
bool *visited = new bool;
for(j=0;j<n;j++)
{
visited[j] = false;
}
adj = new list<int>[n];
for(j=0;j<i;j++)
{
cin>>a>>b;
addEdge(adj, a,b);
}
for(k=0;k<n;k++)
{
if(visited[k]==false)
{
count = 1;
arr[m] = dfsVisit(k, visited, adj);
m++;
}
}
/*for(k=0;k<m;k++)
{
cout<<arr[k];
}*/
long int ans = 1;
if(m>1)
{
for(k=0;k<m;k++)
{
ans = ans * arr[k];
}
}
if(m==1)
{
ans = 0;
}
cout<<ans;
}
答案 0 :(得分:1)
您刚刚声明被访问为指针,但只为其分配了一个内存插槽。
bool *visited = new bool;
for(j=0;j<n;j++)
{
visited[j] = false;
}
答案 1 :(得分:1)
这更多地是关于如何设置代码的样式以及使用c ++提供的工具来避免某些问题的c ++功能。
C ++确实比C更不容易出错,应该使用它。
#include <iostream>
#include <list>
#include <cmath>
#include <vector>
#include <cstddef>
using namespace std;
void addEdge(std::vector<std::list<std::size_t>>& adj,
std::size_t a, std::size_t b)
{
adj.at(a).push_back(b);
}
void dfsVisit(std::size_t i, std::vector<bool>& visited,
std::vector<std::list<std::size_t>> const& adj, std::size_t& count)
{
visited[i] = true;
for (auto k : adj.at(i))
{
if (!visited.at(k))
{
++count;
dfsVisit(k, visited, adj, count);
}
}
}
int main()
{
// initialize all variables and make the scope as small as possible
std::size_t ni{}, ii{};
// try to read n and i from cin
// without the if you never know that you'll actually have valid input
if (std::cin >> ni >> ii)
{
// const refs to block changes
auto const& n = ni;
auto const& i = ii;
// dynamic, contiguous memory ? -> std::vector !
// create visited vector
// no need to loop: we can make them all false here
auto visited = std::vector<bool>(n, false);
// create adj vector
auto adj = std::vector<std::list<std::size_t>>(n);
// you don't need arr on the stack so don't put it there
std::vector<std::size_t> arr;
// we know we'll have at max n entries in arr
arr.reserve(n);
// add edges
for (std::size_t j = 0u; j < i; ++j)
{
std::size_t a{}, b{};
if (std::cin >> a >> b)
{
addEdge(adj, a, b);
}
// TODO: else clause when input fails...
}
std::size_t count{};
for (std::size_t k = 0u; k < n; ++k)
{
if (!visited[k])
{
std::size_t count = 1u;
dfsVisit(k, visited, adj, count);
// we know k < n and m < k since we increment at max once
// so no at but operator[]
arr.push_back(count);
}
}
std::size_t ans = 1u;
auto const m = arr.size();
if (m > 1u)
{
for (std::size_t k = 0; k < m; ++k)
{
ans = ans * arr[k];
}
}
else if (m == 1)
{
ans = 0u;
}
std::cout << ans;
}
// TODO: else clause when input fails...
}