0

Here is my question

Implement a malloc-like memory allocation library.

  1. Declare an array of 25000 bytes.

  2. You must implement a function that is like malloc(). Call it MyMalloc(). Its signature is similar to malloc(). You should also implement MyFree() which has a signature and functionality that are similar to free().

  3. MyMalloc() allocates memory only from the previously mentioned array of 25000 bytes.

  4. All the data structures that are required to manage the memory must also reside within the same array.

  5. MyMalloc() and MyFree() must be in a file called mymalloc.c. You should also provide a suitable header file mymalloc.h.

  6. Our test program contains the main() and it includes mymalloc.h. Do not include a main() in your mymalloc.c.

And here is my soultaion

#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#define MX 25000
#define allocate 'a'
#define notallocate 'f'
#define meta 5


char* mymalloc(int size);
void myfree(char* address);
char* findlocation(int size);
void split(char* ptr,int size);

char memory[MX]={'\0'};
char* base=memory;
int total=0;
int freeby=0;
char* mymalloc(int size){
  if(size<=0){
            printf("Error:Size cant be minius value\n");
            exit(0);
           return NULL;
  }
   if(!*base){
           *base=notallocate;
           *(int*)(base+1)=MX-5;
}

           char* ptr=findlocation(size);
  if(ptr)
  {
          if(*(int*)(ptr+1)>=size){
          split(ptr,size);
    }
}else{
    return NULL;
}
    //total+=(size+5);
    printf("Memory allocation succeed Address:%p:::Allocated size 
   is:%d\n",ptr,size);
   return (ptr);
}

  void myfree(char* address)
 {
      char* temp,*next,*pre,*holder;
      temp=base;int size;
    
      while(true)
      {
        size=*(int*)(temp+1);
        if(temp==address)
      {
        freeby+=size=*(int*)(temp+1);
        break;
      }
        else if(!*temp)
      {
        return;
    }
       else
    {
        pre=temp;
        temp=temp+size;
        next=temp+*(int*)(temp+1);
     }
    }
    freeby+=size=*(int*)(temp+1);
    if(*next==notallocate){
       *temp=notallocate;
       *(int*)(temp+1)=*(int*)(temp+1)+*(int*)(next+1);
     }


   if(*pre==notallocate)
  {
    *temp=notallocate;``
    *(int*)(pre+1)=*(int*)(temp+1)+*(int*)(pre+1);
  }else{
      *address=notallocate;

  } 
   printf("Memory freed sucessfully:%p:::Freed size:%d\n",address,size-5);
  }

   char* findlocation(int size)
  {
   char* temp=base;
   int freesize=0;
   while(true)
  {
    freesize=*(int*)(temp+1);
    if(*temp==notallocate && freesize>=size+5 )
    {
        return (temp);
    }else if(!temp){
        return NULL;
    }else{
        if(temp+freesize+size>=memory+24999)
        {
            printf("Error!sized excessed memory limit \n");
            exit(0);
            return NULL;
        }
        temp=temp+freesize;
      }
    
   }
    return (temp);
 }


 void split(char* ptr,int size)
 {
    char* new;
    int blocksize=*(int*)(ptr+1);
    blocksize=blocksize-size-meta;
    new=ptr+size+meta;
    *new=notallocate;
    *(int*)(new+1)=blocksize;
    *(int*)(ptr+1)=size+meta;
    *ptr=allocate;

  }


void main(){
char* addr1=mymalloc(8);
char* addr2=mymalloc(400);
char* addr3=mymalloc(100);
char* addr4=mymalloc(200);
char* addr5=mymalloc(300);
char* addr6=mymalloc(400);
char* addr7=mymalloc(500);
char* addr10=mymalloc(8);
//printf("8 %p\n",addr1);
//printf("400 %p\n",addr2);
//printf("100 %p\n",addr3);
//printf("200 %p\n",addr4);
//printf("1300 %p\n",addr5);
//printf("400 %p\n",addr6);
//printf("500 %p\n",addr7);
 myfree(addr1);
 myfree(addr2);
 myfree(addr7);
 myfree(addr6);
  //printf("Afrer freeeing\n");
  char* addr8=mymalloc(1300);
  //printf("1300 %p\n",addr8);
    //char* addr10=mymalloc(8);
  //printf("1300 %p\n",addr10);
 //char* addr5=mymalloc(120);
}

Code is just working fine in Dev c++ in windows. But in Linux If I try to free the first allocated block(addr1) and then try to reallocate something it gives me segmentation failed(core dumped). I have searched about this error and find out that it because I am reading into unallocated pointers. Tried almost a day to debug it.But Still couldn't find where is bug. Any suggestion ?

Updated: Using GDB debugger I found out that in the following part of code I am getting and segmentation failed.No idea how to fix it

Error found on debugger

Kalana Mihiranga
  • 488
  • 7
  • 18
  • Possible duplicate of [How to debug segmentation fault?](https://stackoverflow.com/questions/29324796/how-to-debug-segmentation-fault) – rghome Oct 28 '18 at 10:18
  • You need to learn how to use a debugger. It is caused by an invalid pointer (often zero). To expand on that - it doesn't help anyone to point out where the fault is in this case. This is a typical programming error in C and you just have to step through it with a debugger or add printf statements until you figure out where it crashes and why. – rghome Oct 28 '18 at 10:19
  • 1
    It is not compliant to declare an array of char and make other objects alias its contents – Antti Haapala -- Слава Україні Oct 28 '18 at 10:31
  • True, it is a very dangerous thing to cast a char array into an int...but strictly speaking, on some devices, it is "discouraged but permissible...use at your own risk". I haven't figured out what else is wrong ...it's a pretty complicated little piece of code. Clearly though there is a lot of potential for error in this kind of code usage so it's worth a careful look! – ComputerNerd Oct 28 '18 at 10:44
  • @AnttiHaapala: A program that uses a `char` array for other objects may be conforming. It is merely not strictly conforming. Since this appears to be an instruction given as an assigned project, the student is entitled to assume the C implementation being used for the course supports it, for the purpose of the assignment. This is permitted by the C standard, which invites extensions to the language, including defining behavior not defined by the standard. – Eric Postpischil Oct 28 '18 at 10:59
  • @EricPostpischil that is true as well. However most compilers used now actually *don't* permit this - you just can get away with it. – Antti Haapala -- Слава Україні Oct 28 '18 at 11:03
  • @AnttiHaapala: Most compilers do support it, with switches to disable optimization or to permit aliasing. – Eric Postpischil Oct 28 '18 at 12:04

0 Answers0