0

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!");
}
econCodergirl
  • 315
  • 1
  • 3
  • 8
  • Don't follow that tutorial. It's just wrong. Missing `#include mymalloc.h` in the main file. – kaylum Oct 20 '20 at 21:53
  • @kaylum Thanks, I added that into the main file but still get the same error :( – econCodergirl Oct 20 '20 at 21:55
  • And your build commands are not correct. Replace with a single command to link in all the files: `gcc -o memgrind mymalloc.c memgrind.c` – kaylum Oct 20 '20 at 21:55
  • We need to see your exact code. It is not good enough to say "same as over there". You may have typed something wrong. Or made some other small error that you don't even know about. – kaylum Oct 20 '20 at 21:56
  • 1
    You only showed the error message: `int *p=(int)MyMalloc(100*sizeof(int));` is incorrect cast. `(int)` is not `(int*)`. Also please see [Do I cast the result of malloc?](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Weather Vane Oct 20 '20 at 21:56
  • 3
    Wow, that tutorial is very very low quality - full of mistakes. Really, stop using it. – kaylum Oct 20 '20 at 21:58
  • Yes, all 5 calls to `MyMalloc()` in the linked "full code" `main()` use an incorrect cast. – Weather Vane Oct 20 '20 at 22:01
  • I repeat: You need to show the **exact** code you are compiling. In programming all the details matter. We can't effectively help with code we can't see. – kaylum Oct 20 '20 at 22:07
  • @kaylum have added the code now. thanks again for your help – econCodergirl Oct 20 '20 at 22:13
  • You need to put [include guards](https://en.wikipedia.org/wiki/Include_guard) in your header file. No offense, but I would suggest you pause your programming and review a good C book or tutorial to learn the language fundamentals first. Then try simpler code and work up to this more complex code once you have a firmer foundation. – kaylum Oct 20 '20 at 22:19
  • @kaylum Fair enough. But I'm just confused what you mean when you say I need to include guards - I did #include #include - what exactly am I missing? – econCodergirl Oct 20 '20 at 22:24
  • Read the link which tells you how to use include guards. But sorry that's not actually the the problem. The problem is you have this in the header file: `struct block *freeList=(void*)memory;`. So every time you include the header a new `freeList` variable is defined. Since the header is included in both C files the variable is defined twice hence the multiply defined error. Such variable definitions should go into a single C file. If it needs to be used in another file the header should have an `extern` declaration. Again, these are all fundamentals of the C language that you should review. – kaylum Oct 20 '20 at 22:37
  • @kaylum thank you so much. i will review C because I certainly do have some lags. Appreciate it. – econCodergirl Oct 20 '20 at 22:56

1 Answers1

1

the warining says that you try to put int into int * so try this

int *p=(int*)MyMalloc(100*sizeof(int));
Ibram Reda
  • 1,882
  • 2
  • 7
  • 18