Skip to content

Instantly share code, notes, and snippets.

@justinsb
Created January 18, 2012 19:56
Show Gist options
  • Save justinsb/1635174 to your computer and use it in GitHub Desktop.
Save justinsb/1635174 to your computer and use it in GitHub Desktop.
mkdir test
cd test
cat > test.cc <<EOF
#include <memory>
#include <iostream>
using namespace std;
static const size_t kMaxSize = ~static_cast<size_t>(0);
static const size_t kNotTooBig = 100000;
// We want an allocation that is definitely more than main memory. OS
// X has special logic to discard very big allocs before even passing
// the request along to the user-defined memory allocator; we're not
// interested in testing their logic, so we have to make sure we're
// not *too* big.
static const size_t kTooBig = kMaxSize - 100000;
int main() {
try {
char * p = new char[kTooBig];
cout << "Allocation OK\n";
}
catch (std::bad_alloc& a) {
cout << "Allocation threw bad alloc\n";
}
return 0;
}
EOF
g++ -m32 test.cc; ./a.out
# Allocation threw bad alloc
g++ -m64 test.cc; ./a.out
# Segmentation Fault (core dumped)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment