Question
Memory fragmentation in C:Design, implement, and execute a C-program that does the following:It allocates memory for a sequence of 3m arrays of size 800,000 elements each:then it explicitly deallocates all even-numbered arrays and allocates a sequence of arrays of size 1,150,000 elements each. Measure the amounts of time your program requires for the allocation of the first sequence and for the second sequence. Choose m so that you exhaust almost all of the main memory available to your program Explain your timings!!
Explanation / Answer
#include #include #include #include int main() { //FIRST SEQUENCE int *a, *b, *c, *d, *e, *f, *g, *h, *i, *j, *k, *l, start, end; start = (int)GetTickCount(); a = (int *)malloc(500000 * sizeof(int)); if(a == NULL) exit(1); b = (int *)malloc(500000 * sizeof(int)); if(b == NULL) exit(1); c = (int *)malloc(500000 * sizeof(int)); if(c == NULL) exit(1); d = (int *)malloc(500000 * sizeof(int)); if(d == NULL) exit(1); e = (int *)malloc(500000 * sizeof(int)); if(e == NULL) exit(1); f = (int *)malloc(500000 * sizeof(int)); if(f == NULL) exit(1); g = (int *)malloc(500000 * sizeof(int)); if(g == NULL) exit(1); h = (int *)malloc(500000 * sizeof(int)); if(h == NULL) exit(1); free(b); i = (int *)malloc(500000 * sizeof(int)); if(i == NULL) exit(1); j = (int *)malloc(500000 * sizeof(int)); if(j == NULL) exit(1); k = (int *)malloc(500000 * sizeof(int)); if(k == NULL) exit(1); l = (int *)malloc(500000 * sizeof(int)); if(l == NULL) exit(1); end = (int)GetTickCount(); printf("%d ", (end - start)); getch(); free(b); free(d); free(f); free(h); free(j); free(l); ////SECOND SEQUENCE start = (int)GetTickCount(); b = (int *)malloc(700000 * sizeof(int)); if(b == NULL) exit(1); d = (int *)malloc(700000 * sizeof(int)); if(d == NULL) exit(1); f = (int *)malloc(700000 * sizeof(int)); if(f == NULL) exit(1); h = (int *)malloc(700000 * sizeof(int)); if(h == NULL) exit(1); end = (int)GetTickCount(); printf("%d", (end - start)); getch(); return 0; }