I'm sure it's just a simple syntax error. I understand this code has its share of errors, however right now I'm just concerned with the error at the malloc call if structure. I would like to solve the rest of the problems myself. I just cannot seem to find where I have gone wrong with with this particular one. Error should be in line 8.
Thank you!Code:void *myMalloc(unsigned int num_bytes) { header *p; //Pointers to current and previous headers. header temp; if(base_ptr == NULL) //If myMalloc has not been called in program yet. { /***********ERROR LINE BELOW**************/ if((base_ptr = (header*)malloc(MEM_SIZE+(MEM_SIZE%sizeof(header))+sizeof(header))) == NULL) //MEM_SIZE header units. { printf("Malloc fail\n");//Malloc failed to allocated requested memory. return NULL; } base_ptr[0].size = MEM_SIZE; //Sets the initial block equal to the total size set by MEM_SIZE. base_ptr[0].status = FREE; //Declares the initial block as free memory. base_ptr[0].next = NULL; //No other block so the first block's ptr field is NULL. base_ptr[0].prev = NULL; } p = base_ptr; //Set's the current header pointer to the first header. while(p != NULL) //While the point has not reach the end of the list. { if((p->status == FREE) && (p->size >= num_bytes)) //If the current field is free. { temp.size = p->size; temp.prev = p->prev; p->size = num_bytes; //Size of payload. p->status = USED; //Memory block is now allocated. /*If the requested size is exactly equal to the free block, pointers stays the same.*/ if(temp.size > num_bytes) { p->next = (char*)p + (numbytes*sizeof(header)); //Cast to char pointer to handle pointer arithmetic. p->next->size = temp.size - num_bytes; //Cause errors at end of memory? p->next->status = FREE; p->next->prev = p; p->next->next = temp.next; } return (char*)p + sizeof(header); } p = p->next; } printf("myMalloc cannot allocate the requested amount of memory\n"); return NULL; }