c - I am working on this code to allocate some memory and return pointer, but I am getting segmentation fault error -


i working on code allocate memory , return pointer, getting segmentation fault error. please me figure out.

#include <stdio.h> #include <stdlib.h> #include "memalloc.h" int total_holes,sizeofmemory; void* start_of_memory; void setup( int malloc_type, int mem_size, void* start_of_memory ) { /**  *  fill code here  * **/     sizeofmemory=mem_size;     //initionlize memory     start_of_memory = (int *) malloc(mem_size*sizeof(int));     if(malloc_type==0)     {         //first of         printf("first fit");         void firstfit();     }     else if(malloc_type==1)     {         //first of         printf("best fit");         void bestfit();     }     else if(malloc_type==2)     {         //first of         printf("worst fit of");         void worstfit();     }     else if(malloc_type==3)     {         //first of         printf("buddy system");         void buddyfit();     }   }  void *my_malloc(int size) { /**  *  fill code here  * **/      //chek pointer in null or not     if((start_of_memory = malloc(size)) == null) {         printf("no memory reserve");         }         else{             //add more memory in void pointer              start_of_memory=start_of_memory+size;         }                return (void*)-1; }  void my_free(void *ptr) { /**  *  fill code here  * **/     free(ptr); }  int num_free_bytes() { /**  *  fill code here  * **/     //count number of free bytes     int sum=0;     for(int i=0;i<sizeofmemory;i++)     {         if(start_of_memory+i==0)         {             sum++;         }     }     return sum; }  int num_holes() { /**  *  fill code here  * **/     // call function num_free_bytes , check free space     total_holes=num_free_bytes();     return total_holes; } //memalloc.h  void setup(int malloc_type, int mem_size, void* start_of_memory); void *my_malloc(int size); void my_free(void* ptr); int num_free_bytes(); int num_holes(); #define first_fit       0 #define best_fit        1 #define worst_fit       2 #define buddy_system    3 

the code below possibly closer want. custom malloc function returns pointer beginning of allocated memory not end of it. original function never returns allocated memory (void *)(-1)

void *my_malloc(int size) {  void * start_of_memory; //check pointer if null or not if((start_of_memory = (void *)malloc(size)) == null) {     printf("no memory reserve");         return null; // no memory      }     else{              return (start_of_memory);      } 

}


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -