简体   繁体   English

Malloc中的编译错误

[英]Compiling errors in Malloc

I'm following this tutorial to write my own version of malloc and free.我正在按照本教程编写我自己的 malloc 和 free 版本。 The full source code is here which I'm trying to run on my laptop.完整的源代码在这里,我试图在我的笔记本电脑上运行。

I have 3 files:我有3个文件:

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?但我只在 mymalloc.h 中声明一次?

For reference, here is the code:作为参考,这里是代码:

mymalloc.c 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 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 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!");
}

警告说你试图把int放入int *所以试试这个

int *p=(int*)MyMalloc(100*sizeof(int));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM