Question: Problem: I am to code using a C-program. I am doing a memory fragmentation in C++: Design, implement, and execute a C-program that does the

Problem: I am to code using a C-program. I am doing a memory fragmentation in C++: Design, implement, and execute a C-program that does the following: 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 1,000,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 we exhaust almost all of the main memory available to your program. Explain you timings!

Please edit by adding/deleting or making any correction to my code 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); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!