bad_alloc.cc
author Markus Bröker <mbroeker@largo.dyndns.tv>
Sat, 13 Dec 2008 17:57:57 +0100
changeset 5 d752cbe8208e
parent 2 97beb75e5ac7
child 8 96d16dfe787a
permissions -rw-r--r--
asm and parser demos added 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
        }
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
    catch (exception & e) {
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
        cout << e.what () << " after " << i << " iterations" << endl << endl;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
        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
    39
        cin >> action;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
		v.resize(0);
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
        switch (action) {
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
        case 'y':
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
            throw;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
            break;
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
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    48
    return EXIT_SUCCESS;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    49
}