bad_alloc.cc
author Markus Bröker <mbroeker@largo.dyndns.tv>
Sat, 13 Dec 2008 17:58:00 +0100
changeset 8 96d16dfe787a
parent 2 97beb75e5ac7
child 9 c3fecc82ade6
permissions -rw-r--r--
We use return EXIT_SUCCESS instead of return 0 committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
#include <iostream>
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     2
#include <vector>
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     3
#include <exception>
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
#include <sys/time.h>
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
#include <sys/resource.h>
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
using namespace std;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
int set_limit (int megs)
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
{
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
    struct rlimit rlim;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
    rlim.rlim_cur = megs;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
    rlim.rlim_max = 1.25 * megs;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
    if (megs > (4 * 1024 * 1024))
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
        return setrlimit (RLIMIT_AS, &rlim);
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
    return EXIT_FAILURE;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
}
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
int main (int argc, char **argv)
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
{
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
    vector < int >v;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
    int i = 0;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
    char action;
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
    if (set_limit (20 * 1024 * 1024)) {
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
        cout << "System error" << endl;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
        return EXIT_FAILURE;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
    }
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
    try {
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
        for (;;) {
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
            v.push_back (i++);
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
        }
8
96d16dfe787a We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 2
diff changeset
    35
    } catch (exception & e) {
2
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
        cout << e.what () << " after " << i << " iterations" << endl << endl;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
        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
    38
        cin >> action;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
		v.resize(0);
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
        switch (action) {
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
        case 'y':
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
            throw;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
            break;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
        }
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
    }
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
    return EXIT_SUCCESS;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    48
}