// Source: "Software Design ...", John A Robinson, Newnes, 2004, page 161. #include using namespace std; int main() { int i(0); char *p[4*1024]; try { while(i < 4*1024) { p[i++] = new char[1024*1024]; cout << "Round " << i << " allocated starting at "; cout << (unsigned int) p[i-1] << endl; } cout << "Successfully allocated 4 Gigabytes\n"; } catch(bad_alloc) { cout << "Out of memory after " << i << " Megabytes\n"; } for (int j = 0; j < i; j++) delete [] p[j]; return 0; }