author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Sat, 13 Dec 2008 17:58:08 +0100 | |
changeset 16 | 2b4ef74a16aa |
parent 13 | 2640a7fd96ca |
child 28 | 54addf5893ef |
permissions | -rw-r--r-- |
9
c3fecc82ade6
standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
8
diff
changeset
|
1 |
/** |
c3fecc82ade6
standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
8
diff
changeset
|
2 |
* test/demos/bad_alloc.cc |
c3fecc82ade6
standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
8
diff
changeset
|
3 |
* Copyright (C) 2008 Markus Broeker |
c3fecc82ade6
standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
8
diff
changeset
|
4 |
*/ |
c3fecc82ade6
standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
8
diff
changeset
|
5 |
|
2
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
6 |
#include <iostream> |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
7 |
#include <vector> |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
8 |
#include <exception> |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
9 |
#include <sys/time.h> |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
10 |
#include <sys/resource.h> |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
11 |
|
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
12 |
using namespace std; |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
13 |
|
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
14 |
int set_limit (int megs) |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
15 |
{ |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
16 |
struct rlimit rlim; |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
17 |
|
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
18 |
rlim.rlim_cur = megs; |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
19 |
rlim.rlim_max = 1.25 * megs; |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
20 |
if (megs > (4 * 1024 * 1024)) |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
21 |
return setrlimit (RLIMIT_AS, &rlim); |
13
2640a7fd96ca
clear() is better than resize(0)
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
22 |
|
2
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
23 |
return EXIT_FAILURE; |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
24 |
} |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
25 |
|
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
26 |
int main (int argc, char **argv) |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
27 |
{ |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
28 |
vector < int >v; |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
29 |
int i = 0; |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
30 |
char action; |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
31 |
|
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
32 |
if (set_limit (20 * 1024 * 1024)) { |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
33 |
cout << "System error" << endl; |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
34 |
return EXIT_FAILURE; |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
35 |
} |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
36 |
|
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
37 |
try { |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
38 |
for (;;) { |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
39 |
v.push_back (i++); |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
40 |
} |
8
96d16dfe787a
We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
2
diff
changeset
|
41 |
} catch (exception & e) { |
2
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
42 |
cout << e.what () << " after " << i << " iterations" << endl << endl; |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
43 |
cout << "Would you like to see the full error message? (y/n) "; |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
44 |
cin >> action; |
13
2640a7fd96ca
clear() is better than resize(0)
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
45 |
v.clear(); |
2
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
46 |
switch (action) { |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
47 |
case 'y': |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
48 |
throw; |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
49 |
break; |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
50 |
} |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
51 |
} |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
52 |
|
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
53 |
return EXIT_SUCCESS; |
97beb75e5ac7
bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
54 |
} |