bad_alloc.cc
author Markus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:47:18 +0200
changeset 28 54addf5893ef
parent 13 2640a7fd96ca
child 29 7abf6146898e
permissions -rw-r--r--
cstdlib declares EXIT_SUCCESS and EXIT_FAILURE in c++ 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
/**
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>
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;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
    int i = 0;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
    char action;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
    if (set_limit (20 * 1024 * 1024)) {
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
        cout << "System error" << endl;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
        return EXIT_FAILURE;
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
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
    try {
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
        for (;;) {
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
            v.push_back (i++);
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
        }
8
96d16dfe787a We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 2
diff changeset
    42
    } catch (exception & e) {
2
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
        cout << e.what () << " after " << i << " iterations" << endl << endl;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
        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
    45
        cin >> action;
13
2640a7fd96ca clear() is better than resize(0)
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 9
diff changeset
    46
        v.clear();
2
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
        switch (action) {
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    48
        case 'y':
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    49
            throw;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
            break;
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
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    54
    return EXIT_SUCCESS;
97beb75e5ac7 bad alloc example added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
}