Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm not sure to understand your problem but why don't you try something like:</p> <pre><code>#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-&gt;next != 0) tmp = tmp-&gt;next; struct myblock *new_elem = tmp; //your question I guess new_elem-&gt;prev = tmp; new_elem-&gt;size = size; new_elem-&gt;buffer = new_elem + sizeof(*new_elem); new_elem-&gt;next = new_elem-&gt;buffer + new_elem-&gt;size; return (new_elem-&gt;buffer); } int main() { char *str1 = malloc(10); char *str2 = malloc(10); strcpy(str1, "Hello"); strcpy(str2, "World"); printf("%s %s\n", str1, str2); } </code></pre> <p>You should just think your memory in a different way I guess, where inside your heap_space you can have many things.</p> <p>If you don't understand something please ask.</p> <p>You should also use void * and unsigned int instead of int</p> <p>Furthermore you still have some stuff to do: </p> <ul> <li>Check if the size required is available in your array </li> <li>Give a little more space in case you want to implement your realloc </li> <li>Implement your free function</li> </ul> <p>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)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload