Here is my question
Implement a malloc-like memory allocation library.
Declare an array of 25000 bytes.
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().
MyMalloc() allocates memory only from the previously mentioned array of 25000 bytes.
All the data structures that are required to manage the memory must also reside within the same array.
MyMalloc() and MyFree() must be in a file called mymalloc.c. You should also provide a suitable header file mymalloc.h.
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