// ---------------
// StackVsHeap.c++
// ---------------

#include <cassert>  // assert
#include <cstring>  // strcmp
#include <iostream> // cout, endl

int f (int n) {
    if (n == 0)
        return 0;
    return 1 + f(n - 1);}

int main () {
    using namespace std;
    cout << "StackVsHeap.c++" << endl;

    {
    int i = 2;
    ++i;
    assert(i == 3);
    }

    {
    int* p = new int(2);
//  ++p;
    ++*p;
    assert(*p == 3);
    delete p;
    }

    try {
        const int* const a = new int[999999999999999LL];
        assert(false);}
    catch (const bad_alloc& e) {
        #ifdef __APPLE__
            assert(strcmp(e.what(), "St9bad_alloc") == 0);
        #else
            assert(strcmp(e.what(), "std::bad_alloc") == 0);
        #endif
        }

    {
    const int n = 999999;                                    // 9999999 will fail
    assert(f(n) == n);
    }

    cout << "Done." << endl;
    return 0;}

// StackVsHeap.app(325) malloc: *** mmap(size=2467954688) failed (error code=12)
// *** error: can't allocate region
// *** set a breakpoint in malloc_error_break to debug

// Segmentation fault


syntax highlighted by Code2HTML, v. 0.9.1