0

I am trying to create my own malloc but I am stuck on one point. As we know we have to assign struct as a meta data in available space as it is mentioned in this picture. enter image description here

char heap_space[MEM_BUFFER];    
struct myblock
    {
        struct myblock *next;
        struct myblock *prev;
        int size;
        char *buffer;
    }

I have my heap_space which will be my "RAM" . Now I am stuck on one point:-

How to assign my structure myblock to heap_space, and one thing which we should keep in mind that every time when new request will come, the place of the myblock will be changed as per allocated (requested) space.

2
  • using reinterpret_cast ?? Commented Jul 10, 2013 at 12:59
  • @Alexis it is marked as both Commented Jul 10, 2013 at 13:03

3 Answers 3

1

I'm not sure to understand your problem but why don't you try something like:

#define MEM_BUFFER 4096
#define size_t unsigned int


char heap_space[MEM_BUFFER] = {0};
struct myblock
{
  struct myblock *next;
  struct myblock *prev;
  int size;
  char *buffer;
};

void *malloc(size_t size)
{
  struct myblock *tmp = heap_space;
  if (tmp != 0) // != 0 since NULL is in stdlib
    while (tmp->next != 0)
      tmp = tmp->next;
  struct myblock *new_elem = tmp; //your question I guess
  new_elem->prev = tmp;
  new_elem->size = size;
  new_elem->buffer = new_elem + sizeof(*new_elem);
  new_elem->next = new_elem->buffer + new_elem->size;
  return (new_elem->buffer);
}

int main()
{
  char *str1 = malloc(10);
  char *str2 = malloc(10);

  strcpy(str1, "Hello");
  strcpy(str2, "World");

  printf("%s %s\n", str1, str2);
}

You should just think your memory in a different way I guess, where inside your heap_space you can have many things.

If you don't understand something please ask.

You should also use void * and unsigned int instead of int

Furthermore you still have some stuff to do:

  • Check if the size required is available in your array
  • Give a little more space in case you want to implement your realloc
  • Implement your free function

And if you are on linux, you should try to use brk/sbrk instead of having your 'heap space'. But the greatest thing is to run 'real' programs with your own malloc (using LD_PRELOAD)

Sign up to request clarification or add additional context in comments.

3 Comments

by using this way of initilization, i received infinite loop of while tmp->next not null struct myblock *tmp = heap_space; //then loop until your last node struct myblock *new_elem = tmp->next;
@user1642500 I have changed the loop
Thanks @Alexis, Your guidence really helped top fin my solution, although my approch is different but my main problem to understand the initialization of "ram" is solved. struct myblock *new_elem = tmp; new_elem->prev = tmp; Thanks one more time
0

If it's C++, you should use myblock *free_ptr = reinterpret_cast<myblock*>(heap_space); to initialize your free pointer, and then initialize the size, next, prev and buffer of free_ptr.

In C, you would use a regular C style cast, struct myblock *free_ptr = (struct myblock*)heap_space;.

4 Comments

struct myblock free_ptr = (struct myblock)heap_space;. by using this way i could only have space at the begining of array, but how capture space on second or third call. if you look at the picture how where 908 bytes are free, how to assign matadat on this location of array
Yes, you will have to calculate where in the buffer you want that. I'm not here to do your homework tho', you will have to figure out how to do that math. Once you have a char * that points to the right place in the buffer, you can use what I answered (although someone obviously already DID your homework in another answer, so I wonder why I bother at times).
I am enjoying my summer holidays and using my free time to learn new thing in computer... homework ? where did it come from?
Well, whatever your reason for asking is, you are doing it to learn, right? And you don't learn by copy'n'pasting someone else's code, unless you then spend a lot of time working out how it actually works.
0

You should declare

struct myblock
    {
        struct myblock *next;
        struct myblock *prev;
        int size;
        char buffer[0];
    }

so your malloc will return myblockvar.buffer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.