Почему в цикле не генерируется исключение bad_alloc?
[syntax lang="cpp" lines="n"]
#include <iostream>
#include <exception>
#include <new> // чтобы получить доступ к исключению bad_alloc
using namespace std;
int main(){
int *p;
long long unsigned int j;
try{
for(j=0; 1; j++){
//throw 1;
p = new int[32];
}
}
catch(bad_alloc xa){
cout << "bad_alloc:\nj = " << j << endl;
}
catch (exception& e){
cerr << "exception caught: " << e.what() << endl;
}
catch(...){
cout << "Uncnown exception"<< endl;
}
return 0;
}
[/syntax]
Компилятор: mingw-gcc (GCC) 4.7.0
ОС: Win 7 Starter 32-bit
В консоле пишет:
Код: Выделить всё
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Помещение блоков try-catch во внутрь цикла успехом не увенчалось.
[syntax lang="cpp" lines="n"]#include <iostream>
#include <exception>
#include <new> // чтобы получить доступ к исключению bad_alloc
using namespace std;
int main(){
int *p;
long long unsigned int j;
for(j=0; 1; j++){
try{
p = new int[32];
}
catch(bad_alloc xa){
cout << "bad_alloc:\nj = " << j << endl;
break;
}
}
return 0;
}
[/syntax]
Хотя nothrow-версия работает..
и сишный код, решающий ту же задачу, работает
[syntax lang="c" lines="n"]#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int *p;
long long unsigned int j;
for(j=0; 1; j++){
p = (int*)malloc(32 * sizeof(int));
if(p==0){
printf("Больше нельзя. j=%d\n", j);
break;
}
}
return(0);
}[/syntax]
Добавлено спустя 16 часов 34 минуты 40 секунд:
походу баг 4.7.0, т. к. в версии "gcc (TDM-2 mingw32) 4.4.1" исключение исправно генерится