// // part 1 of demo of Linux memory overcommit: // allocate lots of memory, filling as we go // should work // #include #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, filling as we go 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); memset(space, 1, incr*sizeof(*space)); } /* sleep(1); */ } while (space != NULL); // re-fill allocated memory (just to check that all is well) 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; }