Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4096538
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:03:33+00:00 2026-05-20T20:03:33+00:00

I’ve been writing an implementation of malloc and was wondering if someone could help

  • 0

I’ve been writing an implementation of malloc and was wondering if someone could help me with this problem.

Basically, I would like to reuse memory after allocating it using sbrk(), and having made certain that the memory is free.

So essentially, imagine my memory is like this

|------------------------------|

…and I do some allocations. When I allocate memory, each bit has a head (h) and data (d).

|hddddddhddd---hdd--hddd-------|

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?

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

metaStruct * mypointer = the memory address.

But when I try to do

mypointer->size = 30;

Or

mypointer->buddy = 1;

I get a segfault.

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).

How do I go about doing that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T20:03:34+00:00Added an answer on May 20, 2026 at 8:03 pm

    As far as I know, p=sbrk(n) enlarges the available address space of (at least) n bytes and returns the base address of the new allocated area in “p”. So you now have a block of memory starting at “p” and n bytes long (probably more than n, it depends on the system).

    So I suppose that your “metaStruct” contains a “size” field, a “next free area” field, and a “data” field,

    metaStruct * m ;
    p=sbrk(sizeof(metaStruct)+ data_size);
    m = (metaStruct *)p;
    m->size = data_size;
    m->next = NULL;
    memcpy(m->data, ...,data_size);
    

    The code is not perfect, on some systems the sbrk function (indeed it’s often a function, not a basic system call – and of course you should check if sbrk fails) doesn’t return aligned pointers, so you have to align the pointer manually. Also, you can obtain the actual allocated size by calling sbrk(0) after sbrk(n) and calculating the difference between the two pointers. In general, you should mainatin a collection of “free blocks” and try to use them fist, then call sbrk only if none of them is large enough.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.