#include #include /****************************************************** allocates all possible memory by doubling the malloc value until it fails then backing off and incrementing in half steps ensuring memory is used maximally then it idles indefinitely. If total memory is 100 then the first loop does this: malloc(1) malloc(2) malloc(4) malloc(8) malloc(16) malloc(32) malloc(64) malloc(128) <--- fail And the second loop does this: malloc(64+32) malloc(64+32+16) <--- fail malloc(64+32+8) <--- fail malloc(64+32+4) ******************************************************/ int main(void) { char *pch; long int x; long int sum = 0; x = 1; pch = malloc(x * sizeof(char)); while (pch != NULL) { free(pch); x = x * 2; pch = malloc(x * sizeof(char)); } printf("malloc failed at x = %d\n",x); x = x / 2; sum = x; x = x / 2; while (x > 0) { pch = malloc((sum + x) * sizeof(char)); if (pch != NULL) { sum = sum + x; } x = x / 2; free(pch); } printf("sum is %d\n",sum); while (1) ; }