bad_alloc.cc
author Markus Bröker <mbroeker@largo.dyndns.tv>
Mon, 09 Sep 2013 15:51:32 +0200
changeset 166 ecd6492274ad
parent 77 49e0babccb23
permissions -rw-r--r--
Test committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9
c3fecc82ade6 standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 8
diff changeset
     1
/**
77
49e0babccb23 HEADER TAGS
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 29
diff changeset
     2
 * bad_alloc.cc
9
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>
28
54addf5893ef cstdlib declares EXIT_SUCCESS and EXIT_FAILURE in c++
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 13
diff changeset
    11
#include <cstdlib>
2
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
using namespace std;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
int set_limit (int megs)
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
{
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
    struct rlimit rlim;
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
    rlim.rlim_cur = megs;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
    rlim.rlim_max = 1.25 * megs;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
    if (megs > (4 * 1024 * 1024))
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
        return setrlimit (RLIMIT_AS, &rlim);
13
2640a7fd96ca clear() is better than resize(0)
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 9
diff changeset
    23
2
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
    return EXIT_FAILURE;
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
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
int main (int argc, char **argv)
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
{
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
    vector < int >v;
29
7abf6146898e indent -bad changed to -nbad:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 28
diff changeset
    30
2
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
    int i = 0;
29
7abf6146898e indent -bad changed to -nbad:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 28
diff changeset
    32
2
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
    char action;
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
    if (set_limit (20 * 1024 * 1024)) {
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
        cout << "System error" << endl;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
        return EXIT_FAILURE;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
    }
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
    try {
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
        for (;;) {
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
            v.push_back (i++);
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
        }
29
7abf6146898e indent -bad changed to -nbad:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 28
diff changeset
    44
    }
7abf6146898e indent -bad changed to -nbad:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 28
diff changeset
    45
    catch (exception & e) {
2
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
        cout << e.what () << " after " << i << " iterations" << endl << endl;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
        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
    48
        cin >> action;
29
7abf6146898e indent -bad changed to -nbad:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 28
diff changeset
    49
        v.clear ();
2
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
        switch (action) {
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
        case 'y':
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    52
            throw;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    53
            break;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    54
        }
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
    }
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    57
    return EXIT_SUCCESS;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
}