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
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 m arrays of size 900,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.
i have the code but my second allocation is faster than the first which does not make sense. I need help figuring out how to fix this. here is the code
#include
/* Objective: Exhaust the memory to understand fragmentation and explain the timings. * */ int main(int argc, char** argv) { clock_t begin1, stop1, begin2, stop2; double tdif = 0, tdif2 = 0;
for (int k = 0; k < 1000; k++) { double dif, dif2; int *tmpAry; const int m = 500; //Change this begin1 = clock(); printf("Step One "); //DO NOT CHANGE int *container[3 * m]; for (int i = 0; i < (3 * m); i++) { tmpAry = (int *) malloc(800000 * sizeof (int)); container[i] = tmpAry; } stop1 = clock(); printf("Step Two "); //DO NOT CHANGE for (int i = 0; i < (3 * m); i += 2) { free(container[i]); } begin2 = clock(); printf("Step Three "); //DO NOT CHANGE container[m]; for (int i = 0; i < m; i++) { tmpAry = (int *) malloc(900000 * sizeof (int)); container[i] = tmpAry; } stop2 = clock(); dif = (stop1 - begin1) / 1000.00; dif2 = (stop2 - begin2) / 1000.00; tdif += dif; tdif /= 2; tdif2 += dif2; tdif2 /= 2; } printf("First array allocation: %.5f ", tdif); printf("Second array allocation: %.5f ", tdif2); system("pause"); };
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started