出于某种原因,以下程序在打印“我到达这里”之前崩溃。当我注释掉 try-catch 部分时,程序运行并正常退出。
#include <iostream>
int error_function () {
throw 5;
return 0;
}
int main () {
double* b = new double[6];
for (int i = 0; i < 6; i++) {
b[i] = i;
}
double* c = new double(*b);
for (int i = 0; i < 6; i++) {
c[i] = i+1;
}
for (int i = 0; i < 6; i++) {
std::cout << b[i] << " " << c[i] << std::endl;
}
try {
error_function();
}
catch (int t) {
std::cout << "catched an int: " << t << std::endl;
}
std::cout << "i got here" << std::endl;
return 0;
}
这是程序崩溃时我得到的全部输出:
0 1
1 2
2 3
3 4
4 5
5 6
catched an int: 5
*** glibc detected *** ./main: free(): invalid next size (fast): 0x0000000001f22070 ***
======= Backtrace: =========
/lib/libc.so.6(+0x77806)[0x7f2b273a0806]
/lib/libc.so.6(cfree+0x73)[0x7f2b273a70d3]
./main[0x400d92]
(a bunch of stuff)
Aborted
我不知道为什么会这样。任何帮助将不胜感激!