Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 #include #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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Security XI Status And Prospects

Authors: T.Y. Lin, Shelly Qian

1st Edition

0412820900, 978-0412820908

More Books

Students also viewed these Databases questions

Question

b. Why were these values considered important?

Answered: 1 week ago