// // part 2 of demo of Linux memory overcommit: // allocate lots of memory, then fill // will likely fail // #include #include #include #include int main(void) { int * space; const long incr = 1L << 29; std::vector allocated; // allocate memory until we can't get any more int count = 0; do { space = (int *) malloc(incr*sizeof(*space)); printf("call #%d to malloc for 0x%lx bytes returns %p\n", ++count, incr*sizeof(*space), (void *) space); if (space != NULL) { allocated.push_back(space); } } while (space != NULL); // fill allocated memory for (auto s = allocated.begin(); s != allocated.end(); ++s) { printf("initializing space at %p\n", (void *) *s); memset(*s, 1, incr*sizeof(**s)); } return 0; }