Note that there are some explanatory texts on larger screens.

plurals
  1. POMemory allocator in C -- how to utilize sbrk()'ed space
    primarykey
    data
    text
    <p>I've been writing an implementation of malloc and was wondering if someone could help me with this problem.</p> <p>Basically, I would like to reuse memory after allocating it using sbrk(), and having made certain that the memory is free.</p> <p>So essentially, imagine my memory is like this</p> <pre><code>|------------------------------| </code></pre> <p>...and I do some allocations. When I allocate memory, each bit has a head (h) and data (d).</p> <pre><code>|hddddddhddd---hdd--hddd-------| </code></pre> <p>Now I've got these holes, and if I want to use say, the first gap in my diagram, how do I set it up so that it's got a head (h) and a body (dd) also?</p> <p>I've gotten to the point where now I've got a pointer to the memory location I want. In C, its pointed to by a pointer. The pointer has a custom type, where "meta" is a struct I defined. So now I have</p> <pre><code>metaStruct * mypointer = the memory address. </code></pre> <p>But when I try to do</p> <pre><code>mypointer-&gt;size = 30; </code></pre> <p>Or</p> <pre><code>mypointer-&gt;buddy = 1; </code></pre> <p>I get a segfault.</p> <p>The question: how do I set it up so that the memory address, which has been allocated via sbrk(), will have the form of my struct? Obviously I can't just go myPointer = malloc(sizeof(metaStruct)), because I am writing malloc itself. I'm also not interested in sbrk()'ing more space, but rather, utilizing the existing space that I'm pointing to (I want to disregard its junk data and use the space).</p> <p>How do I go about doing that?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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