错误:
malloc.c:2379: 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.
代码:
bool visit[n];
queue<Node<T> > Q;
cout << "*******55551\n";
int s_idx = mp[s.name];
cout << "*******55552\n";
visit[s_idx] = true;
cout << "*******55553\n";
Q.push(s);
cout << "*******55554\n";
Node<T> tmp_s;
cout<<"*******5555\n";
报错:
*******55551
*******55552
*******55553
edgeOverlap: malloc.c:2379: 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)
猜测是内存分配的原因?尝试:
1.指定queue的容器
std::queue<int, std::list<int>> values;
不行,一样的报错
2.使用deque
不行,queue就是用deque实现的,今天才学到 😦
3.clear queue
//clear queue
std::queue<Node<T> > empty;
std::swap(Q, empty);
不行,看来不是内存耗尽,调用结束应该会析构
4.https://stackoverflow.com/questions/64690176/malloc-c2379-sysmalloc-assertion-failed
没有帮助
5.应该是内存泄漏
https://stackoverflow.com/questions/67397359/sysmalloc-assertion-failed-in-c
g++ edgeOverlap.cpp -o edgeOverlap -fsanitize=address
SUMMARY: AddressSanitizer: 11244 byte(s) leaked in 67 allocation(s).
undefined behavior:
https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior
怎么解决呢?
找到原因了,不在这段代码,是set_union里存结果的数组没给足够的空间:
vector<int> unionv_k(2*n); // Neibors(i) union Neibors(j)
std::vector<int>::iterator ls2 = std::set_union(neighbors1_k.begin(), neighbors1_k.end(), neighbors2_k.begin(), neighbors2_k.end(), unionv_k.begin());
下次还是空间开大一点。。。