Reputation: 313
I'm following this tutorial to write my own version of malloc and free. The full source code is here which I'm trying to run on my laptop.
I have 3 files:
mymalloc.h
mymalloc.c
memgrind.c // Where my main method is to test my malloc function
I try to compile it by executing the following on the command line:
gcc -c mymalloc.c
gcc -o memgrind.c memgrind
But I get the following errors:
warning: implicit declaration of function MyMalloc [-Wimplicit-function-declaration]
int *p=(int)MyMalloc(100*sizeof(int));
^~~~~~~~
memgrind.c:5:8: warning: initialization makes pointer from integer without a cast [-Wint-conversion]
int *p=(int)MyMalloc(100*sizeof(int));
Am I compiling my files wrong? Or something wrong with the code? (The code I'm executing & compiling is no different to the source code provided in the link)
Thank you.
EDIT:
Now it is compiling correctly but I get this error:
/tmp/ccKyTOaY.o:(.data.rel.local+0x0): multiple definition of `freeList'
/tmp/cc0jpDeq.o:(.data.rel.local+0x0): first defined here
collect2: error: ld returned 1 exit status
But I'm only declaring it once inside of my mymalloc.h?
For reference, here is the code:
mymalloc.c
#include<stdio.h>
#include<stddef.h>
#include "mymalloc.h"
void initialize(){
freeList->size=20000-sizeof(struct block);
freeList->free=1;
freeList->next=NULL;
}
void split(struct block *fitting_slot,size_t size){
struct block *new=(void*)((void*)fitting_slot+size+sizeof(struct block));
new->size=(fitting_slot->size)-size-sizeof(struct block);
new->free=1;
new->next=fitting_slot->next;
fitting_slot->size=size;
fitting_slot->free=0;
fitting_slot->next=new;
}
void *MyMalloc(size_t noOfBytes){
struct block *curr,*prev;
void *result;
if(!(freeList->size)){
initialize();
printf("Memory initialized\n");
}
curr=freeList;
while((((curr->size)<noOfBytes)||((curr->free)==0))&&(curr->next!=NULL)){
prev=curr;
curr=curr->next;
printf("One block checked\n");
}
if((curr->size)==noOfBytes){
curr->free=0;
result=(void*)(++curr);
printf("Exact fitting block allocated\n");
return result;
}
else if((curr->size)>(noOfBytes+sizeof(struct block))){
split(curr,noOfBytes);
result=(void*)(++curr);
printf("Fitting block allocated with a split\n");
return result;
}
else{
result=NULL;
printf("Sorry. No sufficient memory to allocate\n");
return result;
}
}
void merge(){
struct block *curr,*prev;
curr=freeList;
while((curr->next)!=NULL){
if((curr->free) && (curr->next->free)){
curr->size+=(curr->next->size)+sizeof(struct block);
curr->next=curr->next->next;
}
prev=curr;
curr=curr->next;
}
}
void MyFree(void* ptr){
if(((void*)memory<=ptr)&&(ptr<=(void*)(memory+20000))){
struct block* curr=ptr;
--curr;
curr->free=1;
merge();
}
else printf("Please provide a valid pointer allocated by MyMalloc\n");
}
mymalloc.h
#include<stdio.h>
#include<stddef.h>
char memory[20000];
struct block{
size_t size;
int free;
struct block *next;
};
struct block *freeList=(void*)memory;
void initialize();
void split(struct block *fitting_slot,size_t size);
void *MyMalloc(size_t noOfBytes);
void merge();
void MyFree(void* ptr);
memgrind.c
#include<stdio.h>
#include "mymalloc.h"
int main(){
int *p=(int*)MyMalloc(100*sizeof(int));
char *q=(char*)MyMalloc(250*sizeof(char));
int *r=(int*)MyMalloc(1000*sizeof(int));
MyFree(p);
char *w=(char*)MyMalloc(700);
MyFree(r);
int *k=(int*)MyMalloc(500*sizeof(int));
printf("Allocation and deallocation is done successfully!");
}
Upvotes: 0
Views: 383
Reputation: 2820
the warining says that you try to put int
into int *
so
try this
int *p=(int*)MyMalloc(100*sizeof(int));
Upvotes: 1