forked from ShiqiYu/CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnothrow.cpp
37 lines (32 loc) · 822 Bytes
/
nothrow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
size_t length = 80000000000L;
int * p = NULL;
try {
cout << "Trying to allocate a big block of memory" << endl;
p = new int[length];
//p = new(nothrow) int[length];
cout << "No exception." << endl;
}
catch (std::bad_alloc & ba)
{
cout << "bad_alloc exception!" << endl;
cout << ba.what() << endl;
}
if(p)
cout << "Memory successfully allocated." << endl;
else
cout << "So bad, null pointer." << endl;
// for(size_t i = 0; i < length; i++)
// p[i] = i;
// size_t sum;
// for(size_t i = 0; i < length; i++)
// sum += p[i];
// cout << "Sum = " << sum << endl;
if(p)
delete [] p;
return 0;
}