1 #include <stdlib.h> 2 #include <unistd.h> 3 4 int main(int argc, char *argv[]) { 5 unsigned long i, block_cnt = 100; 6 char** blocks; 7 long page_size; 8 9 page_size = sysconf(_SC_PAGESIZE); 10 if (page_size == -1) { 11 page_size = (1 << 12); // 4Kb 12 } 13 14 if (argc > 1) { 15 block_cnt = strtoul(argv[1], NULL, 10); 16 if (block_cnt < 1) { 17 block_cnt = 1; 18 } 19 } 20 21 blocks = (char**) malloc(block_cnt * sizeof(char*)); 22 for (i = 0; i < block_cnt; i++) { 23 char* dummy_ptr = (char*) malloc(page_size * sizeof(char)); // forcing fragmentation 24 blocks[i] = (char*) malloc(page_size * sizeof(char)); 25 free(dummy_ptr); 26 } 27 28 for (i = 0; i < block_cnt; i++) { 29 char dummy_char = blocks[i][0]; 30 } 31 32 for(i = 0; i < block_cnt; i++) { 33 free(blocks[i]); 34 } 35 36 free(blocks); 37 38 return 0; 39 } 40