Question
Consider this allocator function: static void *find_fit(size_t asize) { /* First fit search */ void *bp; for (bp = heap_listp; GET_SIZE(HDRP(bp)) > 0; bp =
Consider this allocator function:
static void *find_fit(size_t asize) {
/* First fit search */
void *bp;
for (bp = heap_listp; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp)){
if (!GET_ALLOC(HDRP(bp)) && (asize <= GET_SIZE(HDRP(bp)))){
return bp; }
}
return NULL;}
a) Redo the program below using the allocator code "the function given above (find_fit)" . make sure it gives the same results
b) Change the function given above(find_fit) from first fit allocation policy to best fit allocation policy, then redo the program below using the modified allocator code. make sure it gives the same results
your answer should contain two modified copies of the below program,
the first one using the above function as it is, and the other one using the function after changing it from first fit policy to best fit policy "part b"
Program that you will redo in part a and b:
#include
#include
//student structure
struct student {
double marks; //marks
char name[30];//name
int id;//id };
int main() {
int i, noOfRecords=0;
int ch;
struct student **stdArray = NULL;
for(;;)
{ //menu
printf(" 1. Add Student 2. Print All 3. Delete Student 4. Exit Enter your Choice:");
scanf("%d", &ch);//read choice
switch(ch) {
case 1:
stdArray = (struct student **)realloc(stdArray, (noOfRecords + 1) * sizeof(struct student *));//allocate one row
stdArray[noOfRecords] = (struct student *)malloc(sizeof(struct student));//allocate memory for each student
printf("Enter id, name and marks respectively: ");
scanf("%d %s %d", &stdArray[noOfRecords]->id, &stdArray[noOfRecords]->name, &stdArray[noOfRecords]->marks);//read values
noOfRecords+=1;//incremnt count
break;
case 2 :
printf(" Display Student Information: ");
if(noOfRecords==0)//no of records xero that indicates no student data available {
printf(" No student Data available "); }
else {
printf(" Id \t\t Name \t\t Marks ") ;
for(i = 0; i < noOfRecords ; ++i)
printf("%d\t\t%s\t\t%d ",stdArray[i]->id, stdArray[i]->name, stdArray[i]->marks);//print all records }
break;
case 3:
stdArray = (struct student **)realloc(stdArray, (noOfRecords - 1) * sizeof(struct student *));//delete one record
noOfRecords-=1;
break;
case 4:
free(stdArray); //deleted all records
exit(0);}}
return 0;}
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