hi I have copy pasted the code in website too. appreciate your time and help Hi
ID: 3754362 • Letter: H
Question
hi I have copy pasted the code in website too.
appreciate your time and help
Hi Vishal,
Thank You!! //Bro some partial answer would help too...
Here is the format to use for the following two services of the memory manager 1. Report Jobs For each job you should report each block of memory they use. You should report the blocks h order but you do NOT have to report the jobs in order of job number. The Colum Headers should also be printed (the green below) JB Memory Usage 10-50 64-75 200-240 1-9 101-134 180- 184 185-192 2. Report Memory This should be a sequential dump of all memory showing usage Mamory Block JOB 0-9 10-50 51-53 54-75 7-100 101 134 135-179 180-184 185- 192 193-199 200-240 241 255 FREE FREE FREE FREE FREEExplanation / Answer
//Have added codings wherever is required by using (//) symbol for explanation
#include<iostream>
#include<stdlib.h>
using namespace std;
// defining the global variable
int a=5;
int main()
{
//local variable
int a=10;
cout<<"local a: "<<a<<" Global a: "<<::a;
// Re-defining global variable by using ::
::a=15;
cout<<" local a: "<<a<<" Global a: "<<::a;
return 0;
}//declaration of global data structures
typedef struct FREE_NODE * FREEPTR;
typedef struct ALLOCNODE * ALLOCPTR;
struct FREE_NODE // FREE LIST NODES
{
int start_byte;
int end_byte;
int size; FREEPTR next;
};
struct ALLOCNODE // ALLOCTADED LIST NODES
{
int start_byte;
int end_byte;
int size;
int id;
ALLOCPTR next;
};
FREEPTR freelist = NULL; //
the FREE link list ALLOCPTR alloclist = NULL;//allocating list for memory pointer
the ALLOCATED link list int total_memory_managed = 0; // the amount of memory managed //
//prototypes to define
void *calloc(size_t num_elements, size_t element_size}; //to define the prototypes in memory allocation
int //integer elements that are all initially zero
ip = (int *) calloc(100, sizeof(int));
void dump_freelist(void);
void dump_alloclist(void); //allocating dumb code
void remove_empty_freenodes(void); //removing empty free nodes
void insert_freeblock(FREEPTR fptr);
void merge_freenodes(void); //--- interface ---
void init_memory_manager(const int amount);
int allocate_memory(const int job, const int amount);
void release_memory(const int job);
int total_free(void);
int total_allocated(void);
int largest_free(void);
int job_allocated(const int job);
void report_memory(void); //STUB
void report_jobs(void);
void *realloc( void *ptr, size_t new_size);
int main();
{
int main(void)
{ char ch ; // used to pause between tests
int r;
// results of allocate_memory //
struct S {
S() { std::cout << "S::S()" << std::endl; }
S() { std::cout << "S::~S()" << std::endl; }
};
void f() {
S s1;
S *s2 = new (&s1) S;
// ...
delete s2;
}
" << endl << endl;
init_memory_manager(200);
r = allocate_memory(1,200);
cout << "allocate_memory returns :
" << r << endl << endl; // ALL memory r = allocate_memory(2,30);
cout << "allocate_memory returns : " << r << endl << endl; // over allocate release_memory(1);
// free all memory r = allocate_memory(1,-1);
cout << "allocate_memory returns : " << r << endl << endl; // try allocate -1
r = allocate_memory(3,0);
cout << "allocate_memory returns : " << r << endl << endl;
// allocate 0 r = allocate_memory(1,256);
cout << "allocate_memory returns : " << r << endl << endl;
// over allocate r = allocate_memory(1,100);
cout << "allocate_memory returns : " << r << endl << endl; //Ok allocate 100
cout << "total free memory is : " << total_free() << endl; // 100
cout << "total alloc memory is : " << total_allocated() << endl; // 100 release_memory(1);
//free up memory cout << endl;
cout << "ENTER A CHARACTER ";
cin >>ch; cout << endl; /* //
//deallocation of memory init
void f() {
int *i1, *i2;
try {
i1 = new int;
i2 = new int;
} catch (std::bad_alloc &) {
delete i1;
delete i2;
}
}
" << endl << endl;
init_memory_manager(200);
r = allocate_memory(1,20);
cout << "allocate_memory returns : " << r << endl << endl;
r = allocate_memory(2,30);
cout << "allocate_memory returns : " << r << endl << endl;
r = allocate_memory(1,20);
cout << "allocate_memory returns : " << r << endl << endl;
r = allocate_memory(3,30);
cout << "allocate_memory returns : " << r << endl << endl;
r = allocate_memory(1,20);
cout << "allocate_memory returns : " << r << endl << endl;
cout << "total free memory is : " << total_free() << endl;
cout << "total alloc memory is : " << total_allocated() << endl;
release_memory(1);
//release of memory from allocation
int *i1 = nullptr, *i2 = nullptr;
try {
i1 = new int;
i2 = new int;
} catch (std::bad_alloc &) {
delete i1;
delete i2;
}
}
report_memory();
//report_jobs();
//for testing until reports done *** remove from final *** dump_freelist();
dump_alloclist();
cout << "total free memory is : " << total_free() << endl; // 100
cout << "total alloc memory is : " << total_allocated() << endl; // 100
cout << endl; cout << "ENTER A CHARACTER "; cin >>ch; cout << endl; /
//test between merge of blocks
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
/* create temp arrays */
int L[n1], R[n2];
/* Copy data to temp arrays L[] and R[] */
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays back into arr[l..r]*/ to test the merge of blocks
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
remove.finaldumplist();
}
k++;
}
<< endl << endl;
init_memory_manager(200);
r = allocate_memory(1,25);
cout << "allocate_memory returns : " << r << endl << endl;
r = allocate_memory(2,25); cout << "allocate_memory returns : " << r << endl << endl;
r = allocate_memory(3,25); cout << "allocate_memory returns : " << r << endl << endl;
r = allocate_memory(4,25); cout << "allocate_memory returns : " << r << endl << endl;
r = allocate_memory(5,25); cout << "allocate_memory returns : " << r << endl << endl;
<< endl << endl; cout << "total free memory is : " << total_free() << endl;
// 100 cout << "total alloc memory is : " << total_allocated()
<< endl; // 100 release_memory(1); release_memory(3); release_memory(2);
report_memory();
report_jobs();
//dump_freelist();
//dump_alloclist();
cout << endl;
cout << "ENTER A CHARACTER ";
cin >>ch; cout << endl;
//deallocation in merge
int *p = NULL; //request memory for a variable
p = new int;
pointer-variable = new data-type[size];
init_memory_manager(200);
r = allocate_memory(1,20);
cout << "allocate_memory returns : " << r << endl << endl;
r = allocate_memory(3,20);
cout << "allocate_memory returns : " << r << endl << endl;
r = allocate_memory(1,30);
cout << "allocate_memory returns : " << r << endl << endl; //(A)
r = allocate_memory(4,20);
cout << "allocate_memory returns : " << r << endl << endl;
r = allocate_memory(1,20);
cout << "allocate_memory returns : " << r << endl << endl;
r = allocate_memory(1,20); cout << "allocate_memory returns : " << r << endl << endl; // (B)
cout << "total free memory is : " << total_free() << endl;
cout << "total alloc memory is : " << total_allocated() << endl;
release_memory(1)
cout << " total free memory is : " << total_free() << endl; /
cout << "ENTER A CHARACTER "; cin >>ch;
cout << endl; */
return 0;
}
void release_memory(const int job) {
// Free all memory allocated to process ALLOCPTR a1 = alloclist;
ALLOCPTR temp = NULL;
FREEPTR f1 = NULL;
if (a1 == NULL) return; // empty list // remove all first nodes THIS effects the alloclist pointer !!!!!
while(a1 != NULL && a1->id == job) // {
// set up freenode to be inserted f1 = new FREE_NODE; f1->next = NULL;
f1->start_byte = a1->start_byte;
f1->end_byte = a1->end_byte;
f1->size = a1-> size;
// INSERT f1 into FREELIST insert_freeblock(f1);
f1 = NULL; // just to make sure;
// clean up alloclist alloclist = a1->next; delete a1; a1 = alloclist; }
// all first nodes have been deleted if (alloclist == NULL) return;
// empty list while (a1->next != NULL) { while ((a1->next != NULL) && (a1->next->id == job))
{ // set up freenode to be inserted f1 = new FREE_NODE; f1->next = NULL; f1->start_byte = a1->next->start_byte;
f1->end_byte = a1->next->end_byte; f1->size = a1->next-> size; // INSERT f1 into free list insert_freeblock(f1);
f1 =NULL; // just to make sure // clean up alloclist temp = a1->next ; a1->next = temp->next; delete temp; } if (a1->next != NULL) a1 = a1->next; }
return; } /
//testing functions
class TestALLOCATED LIST DUMP :
public BaseTest
ALLOCPTR tmp = alloclist;,
{
public:
Test(tmp!NULL);
void
apply(const float &l1,
const float &l2,
const float &l3, bool &result)
{
move pointer to next node (or NULL)
}
result =
cout << tmp->id << endl;
tmp = tmp->next;
}
const char *getName() const
{
return "Test results";
}
};
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.