gpt4 book ai didi

c++ - 双自由或腐败(出): 0x0000000001a880a0 *** on combinatory algorithm using vectors

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:14:19 26 4
gpt4 key购买 nike

我正在编写一种算法,通过计算每个顶点组合的最小树来计算给定图上的斯坦纳树,但我的组合算法似乎无法正确管理内存使用,但我看不到我在哪里没能做到...

它的工作原理如下,该算法接收一个 vector ,其中包含必须始终包含在图中的顶点,因此第一件事是创建一个包含可选顶点的 vector ,我们将合并的顶点(我使用 10测试海豚),然后它计算从 1 个元素到 k 个元素的组合(k 小于可选顶点集的总大小)。

我们将生成索引的组合(代替直接元素,因为这样更容易),然后将这些索引中的元素包含在 vComb vector 中。

#include <iostream>
#include <vector>
using namespace std;

//Function that calculates the next combination
bool next_comb(vector<int>& indComb, const int k, const int n) {
int i = k - 1;
indComb[i]++;
while((i >= 0) && (indComb[i] >= n-k+1+i)){
i--;
indComb[i]++;
}
if (indComb[0] > n - k) //We've arrived to the combination (n-k, n-k+1, ..., n)
return false; //We can't generate more combinations
//Combination is in form(..., x, n, n, n, ..., n).
//We will put it in form (..., x, x + 1, x + 2, ...)
for (i = i + 1; i < k; ++i)
indComb[i] = indComb[i - 1] + 1;
return true;
}

//Checks if element is in vector
bool is_in(const int n,const vector<int> v){
for(int i=0; i<v.size(); i++)
if(n==v[i]) return true;
return false;
}

void ST(const vector<int> vMandatory){
cout << "V Mandatory: ";
for(int i=0;i<vMandatory.size();i++)
cout << vMandatory[i] << " ";
cout << " " << endl;

//Create and initializate the vector that contains the optional vertices
vector<int> vOptional(10-vMandatory.size());
int j=0;
for(int i=0;i<10;i++){
if(!is_in(i,vMandatory)){
vOptional[j] = i;
j++;
}
}

cout << "V Optional: ";
for(int i=0;i<vOptional.size();i++)
cout << vOptional[i] << " ";
cout << " " << endl;


int k = 1; //Initialize number of elements to combine
int n = vOptional.size(); //Number of elements inside de set to combine
vector<int> indComb; //Vector that will keep the combined indexes of the combination
vector<int> vComb; //Vector that will keep the combined elements
//We control that the number of elements in the combination can't exceed the number of elements in the set
while(k <= vOptional.size()){
indComb.resize(k);
vComb.resize(k);

//Preparare the first combination
for(int i=0;i<k;i++){
indComb[i] = i;
vComb[i] = vOptional[indComb[i]];
}

cout << "V Primera Combinación: ";
for(int i=0;i<vComb.size();i++)
cout << vComb[i] << " ";
cout << " " << endl;

//We make the rest of combinations
while(next_comb(indComb,k,n)){
for(int i=0;i<k;i++)
vComb[i] = vOptional[indComb[i]];

cout << "V Next Combinations: ";
for(int i=0;i<vComb.size();i++)
cout << vComb[i] << " ";
cout << " " << endl;
}
k++;
cout << k << endl;
}
}

int main(){
vector<int> vObligatorios(3);
vObligatorios[0] = 0;
vObligatorios[1] = 1;
vObligatorios[2] = 2;
ST(vObligatorios);
}

这是编译器给我的错误

*** Error in `./a.out': double free or corruption (out): 0x0000000001a880a0 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f8f9a4c67e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x7fe0a)[0x7f8f9a4cee0a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f8f9a4d298c]
./a.out[0x402612]
./a.out[0x4022c4]
./a.out[0x401c98]
./a.out[0x40213e]
./a.out[0x401b78]
./a.out[0x401823]
./a.out[0x4010b4]
./a.out[0x40149c]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f8f9a46f830]
./a.out[0x400bb9]
======= Memory map: ========
00400000-00404000 r-xp 00000000 08:06 14156577 /home/pedro/Desktop/a.out
00603000-00604000 r--p 00003000 08:06 14156577 /home/pedro/Desktop/a.out
00604000-00605000 rw-p 00004000 08:06 14156577 /home/pedro/Desktop/a.out
01a76000-01aa8000 rw-p 00000000 00:00 0 [heap]
7f8f94000000-7f8f94021000 rw-p 00000000 00:00 0
7f8f94021000-7f8f98000000 ---p 00000000 00:00 0
7f8f9a146000-7f8f9a24e000 r-xp 00000000 08:06 2621535 /lib/x86_64-linux-gnu/libm-2.23.so
7f8f9a24e000-7f8f9a44d000 ---p 00108000 08:06 2621535 /lib/x86_64-linux-gnu/libm-2.23.so
7f8f9a44d000-7f8f9a44e000 r--p 00107000 08:06 2621535 /lib/x86_64-linux-gnu/libm-2.23.so
7f8f9a44e000-7f8f9a44f000 rw-p 00108000 08:06 2621535 /lib/x86_64-linux-gnu/libm-2.23.so
7f8f9a44f000-7f8f9a60e000 r-xp 00000000 08:06 2621530 /lib/x86_64-linux-gnu/libc-2.23.so
7f8f9a60e000-7f8f9a80e000 ---p 001bf000 08:06 2621530 /lib/x86_64-linux-gnu/libc-2.23.so
7f8f9a80e000-7f8f9a812000 r--p 001bf000 08:06 2621530 /lib/x86_64-linux-gnu/libc-2.23.so
7f8f9a812000-7f8f9a814000 rw-p 001c3000 08:06 2621530 /lib/x86_64-linux-gnu/libc-2.23.so
7f8f9a814000-7f8f9a818000 rw-p 00000000 00:00 0
7f8f9a818000-7f8f9a82e000 r-xp 00000000 08:06 2625962 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f8f9a82e000-7f8f9aa2d000 ---p 00016000 08:06 2625962 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f8f9aa2d000-7f8f9aa2e000 rw-p 00015000 08:06 2625962 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f8f9aa2e000-7f8f9aba0000 r-xp 00000000 08:06 5244862 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f8f9aba0000-7f8f9ada0000 ---p 00172000 08:06 5244862 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f8f9ada0000-7f8f9adaa000 r--p 00172000 08:06 5244862 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f8f9adaa000-7f8f9adac000 rw-p 0017c000 08:06 5244862 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f8f9adac000-7f8f9adb0000 rw-p 00000000 00:00 0
7f8f9adb0000-7f8f9add6000 r-xp 00000000 08:06 2621445 /lib/x86_64-linux-gnu/ld-2.23.so
7f8f9afb4000-7f8f9afb9000 rw-p 00000000 00:00 0
7f8f9afd2000-7f8f9afd5000 rw-p 00000000 00:00 0
7f8f9afd5000-7f8f9afd6000 r--p 00025000 08:06 2621445 /lib/x86_64-linux-gnu/ld-2.23.so
7f8f9afd6000-7f8f9afd7000 rw-p 00026000 08:06 2621445 /lib/x86_64-linux-gnu/ld-2.23.so
7f8f9afd7000-7f8f9afd8000 rw-p 00000000 00:00 0
7ffd45daf000-7ffd45dd0000 rw-p 00000000 00:00 0 [stack]
7ffd45de6000-7ffd45de8000 r--p 00000000 00:00 0 [vvar]
7ffd45de8000-7ffd45dea000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted (core dumped)

最佳答案

这里:

while((i >= 0) && (indComb[i] >= n-k+1+i)){
i--;
indComb[i]++;
}

你可以让i为负,导致下一行未定义。
为该案例添加一些跟踪以验证它确实发生了。

(您可能会为 vector 的分配存储覆盖内存管理器的内部数据。)

关于c++ - 双自由或腐败(出): 0x0000000001a880a0 *** on combinatory algorithm using vectors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43427273/

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