1#include <list> /* required, to expose allocator */ 2#include <stdexcept> 3#include <stdio.h> 4 5using namespace std; 6 7struct BigStruct 8{ 9 char _data[4096]; 10}; 11 12void bad_alloc_test() 13{ 14 typedef allocator<BigStruct> BigStructAllocType; 15 BigStructAllocType bigStructAlloc; 16 17 try { 18 //Lets try to allocate almost 4096 Go (on most of the platforms) of memory: 19 BigStructAllocType::pointer pbigStruct = bigStructAlloc.allocate(1024 * 1024 * 1024); 20 21 // CPPUNIT_ASSERT( pbigStruct != 0 && "Allocation failed but no exception thrown" ); 22 } 23 catch (bad_alloc const&) { 24 printf( "Ok\n" ); 25 } 26 catch (...) { 27 //We shouldn't be there: 28 // CPPUNIT_ASSERT( false && "Not bad_alloc exception thrown." ); 29 } 30} 31 32void bad_alloc_test1() 33{ 34 try { 35 allocator<BigStruct> all; 36 BigStruct *bs = all.allocate(1024*1024*1024); 37 38 // throw bad_alloc(); 39 } 40 catch ( bad_alloc const & ) { 41 printf( "I am here\n" ); 42 } 43 catch ( ... ) { 44 } 45} 46 47int main() 48{ 49 bad_alloc_test(); 50#if 0 51 try { 52 throw bad_alloc(); 53 } 54 catch ( bad_alloc& ) { 55 } 56 catch ( ... ) { 57 } 58#endif 59 return 0; 60} 61