何らかの理由で、「i got here」が表示される前に次のプログラムがクラッシュします。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
なぜこれが起こっているのか分かりません。どんな助けでも大歓迎です!