Menu

Problem Code Using C Program Memory Fragmentation C Design Implement Execute C Program Fol Q43900237

Problem: I am to code using a C-program. I am doing a memoryfragmentation in C++: Design, implement, and execute a C-programthat does the following: Allocates memory for a sequence of 3marrays of size 800,000 elements each; then it explicitlydeallocates all even-numbered arrays and allocates a sequence of marrays of size 1,000,000 elements each. Measure the amounts of timeyour program requires for the allocation of the first sequence andfor the second sequence. Choose m so that we exhaust almost all ofthe main memory available to your program. Explain you timings!

Please edit by adding/deleting or making any correction to mycode so that it may successfully debug 🙂

Code:

#include “header.h”
void allocate_800k(int** &, int);
void free_memory(int** &, int);
void allocate_900k(int** &, int);

int main()
{
   clock_t start1, end1, start2, end2;
   int m = 3500;
   int size = 3 * m;
   int** array_num1 = (int**)malloc(size);
   int** array_num2 = (int**)malloc(m);
   start1 = clock();
   allocate_800k(array_num1, size);
   end1 = clock();
   free_memory(array_num1, size);
   start2 = clock();
   allocate_900k(array_num2, m);
   end2 = clock();
   double time1 = double(end1 – start1) /CLOCKS_PER_SEC;
   double time2 = double(end2 – start2) /CLOCKS_PER_SEC;
   cout << “First allocation time: ” << time1<< ” ms” << endl;
   cout << “Second allocation time: ” <<time2 << ” ms” << endl;

   system(“pause”);
}
void allocate_800k(int**& array_num1, int size) {
   for (int i = 0; i < size; i++) {
       array_num1[i] =(int*)malloc(800000);
   }
}
void free_memory(int**& array_num1, int size) {
   for (int i = 0; i < size; i += 2) {
       free(array_num[i]);
   }
}
void allocate_900k(int**& array_num2, int size) {
   for (int i = 0; i < size; i++) {
       array_num2[i] =(int*)malloc(900000);
   }
}

Expert Answer


Answer to Problem: I am to code using a C-program. I am doing a memory fragmentation in C++: Design, implement, and execute a C-pr…

OR