Question: fix my code please heres my output: About to dump the free list: Node address: 0 x 7 f 8 a 7 acff 0 5

fix my code please"
heres my output:
About to dump the free list:
Node address: 0x7f8a7acff050 Node size (stored): 3145648 Node size (actual)3145664 Nod e t ( i )
No suitable block found for allocation
Error: Sixth allocation failed 74
Failure in memory test one
main.c
int main(int argc, char** argv){
setlocale(LC_NUMERIC,"");
if (0!= memoryTestOne()){
fprintf(stderr,"Failure in memory test one");
exit (EXIT_FAILURE);
}
if (0!= memoryTestTwo()){
fprintf(stderr,"Failure in memory test two");
exit (EXIT_FAILURE);
}
if (0!= memoryTestThree()){
fprintf(stderr,"Failure in memory test three");
exit (EXIT_FAILURE);
}
printf("Tests Succeeded");
return (EXIT_SUCCESS);
}
int main(int argc, char** argv){
setlocale(LC_NUMERIC,"");
if (0!= memoryTestOne()){
fprintf(stderr,"Failure in memory test one");
exit (EXIT_FAILURE);
}
if (0!= memoryTestTwo()){
fprintf(stderr,"Failure in memory test two");
exit (EXIT_FAILURE);
}
if (0!= memoryTestThree()){
fprintf(stderr,"Failure in memory test three");
exit (EXIT_FAILURE);
}
printf("Tests Succeeded");
return (EXIT_SUCCESS);
}
/* fscMalloc.c *//* fscMalloc.c */
#include "fscMalloc.h"
#include
#include
void* fscMemorySetup(memoryStructure* m, fscAllocationMethod am, size_t sizeInBytes){
if (FIRST_FIT_RETURN_FIRST != am){
fprintf(stderr, "This code only supports the FIRST_FIT_RETURN_FIRST allocation method
");
return NULL;
}
// Allocate memory using malloc
m->head = malloc(sizeInBytes);
if (m->head == NULL){
fprintf(stderr, "Memory allocation failed
");
return NULL;
}
// Initialize memory
m->head->size = sizeInBytes - sizeof(fsc_free_node_t);
m->head->next = NULL;
return m->head;
}
void* fscMalloc(memoryStructure* m, size_t requestedSizeInBytes){
// Search for the first fit
fsc_free_node_t* current = m->head;
fsc_free_node_t* prev = NULL;
while (current != NULL){
if (current->size >= requestedSizeInBytes){
// Allocate memory
if (current->size > requestedSizeInBytes){
// Split the block
fsc_free_node_t* newBlock =(fsc_free_node_t*)((char*)current + requestedSizeInBytes + sizeof(fsc_free_node_t));
newBlock->size = current->size - requestedSizeInBytes - sizeof(fsc_free_node_t);
newBlock->next = current->next;
current->next = newBlock;
}
// Update current block size
current->size = requestedSizeInBytes;
// Remove current block from free list
if (prev == NULL){
m->head = current->next;
} else {
prev->next = current->next;
}
// Return allocated memory
return (void*)(current +1);
}
prev = current;
current = current->next;
}
// No suitable block found
fprintf(stderr,"No suitable block found for allocation
");
return NULL;
}
void fscFree(memoryStructure* m, void * returnedMemory){
// Cast returnedMemory to free_node_t
fsc_free_node_t* block =(fsc_free_node_t*)((char*)returnedMemory - sizeof(fsc_free_node_t));
// Add block to free list
block->next = m->head;
m->head = block;
}
/* Given a node, prints the list for you. */
void printFreeList(FILE * out, fsc_free_node_t* head){
/* given a node, prints the list. */
fsc_free_node_t* current = head;
fprintf(out, "About to dump the free list:
");
while (current != NULL){
fprintf(out, "Node address: %p\t Node size (stored): %zu\t Node size (actual)%zu\t Node next:%p
",
(void*)current,
current->size,
current->size + sizeof(fsc_free_node_t),
(void*)current->next);
current = current->next;
}
}

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 Programming Questions!