0
當我在Ubuntu系統上運行一個簡單的C++程序時,出現雙重空閒或損壞錯誤。這一直困擾着我一段時間。 我標記了給我這個錯誤的那一行。當我評論該行時,我沒有任何錯誤。我怎樣才能擺脫這個錯誤? Plz幫助!C++ double free or corruption錯誤
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
int t;
int count = 0;
string a, b;
vector<int> v(57, 0);
cin >> t;
while(t--) {
cin >> a >> b;
for (int i = 0; i < a.size(); i++) {
v[a[i] - 65]++;
cout << char(a[i]) << " " << v[a[i] - 65] << endl;
}
cout << endl;
for (int j = 0; j < b.size(); j++) {
int id = b[j] - 65;
if (v[id] > 0) {
int abc = v[id];
v[id] = abc - 1; /*****ERROR******/
cout << char(b[j]) << " ";
count++;
}
}
cout << count << endl;
cout << "----" << endl;
count = 0;
}
return 0;
}
進入測試用例的數量和2個字符串作爲輸入之後,我得到以下錯誤:
1
abcd
xyz
a 1
b 1
c 1
d 1
z 1
----
*** Error in `./a.out': double free or corruption (!prev): 0x09db6008 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x767e2)[0xb74b97e2]
/lib/i386-linux-gnu/libc.so.6(+0x77530)[0xb74ba530]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZdlPv+0x1f)[0xb765db4f]
./a.out[0x8049147]
./a.out[0x8049091]
./a.out[0x8048f99]
./a.out[0x8048ef6]
./a.out[0x8048d79]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf5)[0xb745c935]
./a.out[0x80489a1]
======= Memory map: ========
08048000-0804a000 r-xp 00000000 08:05 4195506 /home/vijender/codechef/a.out
0804a000-0804b000 r--p 00001000 08:05 4195506 /home/vijender/codechef/a.out
0804b000-0804c000 rw-p 00002000 08:05 4195506 /home/vijender/codechef/a.out
09db6000-09dd7000 rw-p 00000000 00:00 0 [heap]
b73fe000-b7400000 rw-p 00000000 00:00 0
b7400000-b7441000 r-xp 00000000 08:05 3670121 /lib/i386-linux-gnu/libm-2.17.so
b7441000-b7442000 r--p 00040000 08:05 3670121 /lib/i386-linux-gnu/libm-2.17.so
b7442000-b7443000 rw-p 00041000 08:05 3670121 /lib/i386-linux-gnu/libm-2.17.so
b7443000-b75f1000 r-xp 00000000 08:05 3670122 /lib/i386-linux-gnu/libc-2.17.so
b75f1000-b75f3000 r--p 001ae000 08:05 3670122 /lib/i386-linux-gnu/libc-2.17.so
b75f3000-b75f4000 rw-p 001b0000 08:05 3670122 /lib/i386-linux-gnu/libc-2.17.so
b75f4000-b75f8000 rw-p 00000000 00:00 0
b75f8000-b7613000 r-xp 00000000 08:05 3670878 /lib/i386-linux-gnu/libgcc_s.so.1
b7613000-b7614000 r--p 0001a000 08:05 3670878 /lib/i386-linux-gnu/libgcc_s.so.1
b7614000-b7615000 rw-p 0001b000 08:05 3670878 /lib/i386-linux-gnu/libgcc_s.so.1
b7615000-b76f1000 r-xp 00000000 08:05 268046 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.17
b76f1000-b76f2000 ---p 000dc000 08:05 268046 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.17
b76f2000-b76f6000 r--p 000dc000 08:05 268046 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.17
b76f6000-b76f7000 rw-p 000e0000 08:05 268046 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.17
b76f7000-b76fe000 rw-p 00000000 00:00 0
b7717000-b771c000 rw-p 00000000 00:00 0
b771c000-b771d000 r-xp 00000000 00:00 0 [vdso]
b771d000-b773d000 r-xp 00000000 08:05 367/lib/i386-linux-gnu/ld-2.17.so
b773d000-b773e000 r--p 0001f000 08:05 367/lib/i386-linux-gnu/ld-2.17.so
b773e000-b773f000 rw-p 00020000 08:05 367/lib/i386-linux-gnu/ld-2.17.so
bfa67000-bfa88000 rw-p 00000000 00:00 0 [stack]
Aborted (core dumped)
'id'的價值是什麼? – John3136
如果您使用'v.at(id)'而不是'v [id]',它會給出std :: out_of_range的正確錯誤。 – cdbitesky