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 8772899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:09:00+00:00 2026-06-13T18:09:00+00:00

I am trying to implement Malloc manually in a C project. Here is my

  • 0

I am trying to implement Malloc manually in a C project.
Here is my code:

void *Mem_Alloc(int size) {
    struct Node *p, *prevp = head;

    if (fitPolicy == P_BESTFIT) {

    }

    if (fitPolicy == P_FIRSTFIT) {
        for (p = prevp->next; ;prevp = p, p = p->next) {
            if (p->size >= size) {
                if (p->size == size)
                    prevp->next = p->next;
                else {
                    p->size -= size;
                    p += p->size;
                    // p->size = size;
                }
                head = prevp;
                return (void *)(p+1);
            }
            if (p == head) {
                return NULL;
            }
        }
    }

    if (fitPolicy == P_WORSTFIT) {

    }
    return NULL;
}

Basically, I call mmap in another method called Mem_Init; then, the returned memory map is pointed to by the variable head.
I always get a segmentation fault on the commented part in the middle of Mem_Alloc. I do not know why. Could you guys help me with this? Some hints?

  • 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-06-13T18:09:01+00:00Added an answer on June 13, 2026 at 6:09 pm

    ughoavgfhw has the right direction, but casting lvalue is not appropriate.

    You can use:

    void * tmp_p = (void *)p;
    tmp_p += p->size;
    p = (struct Node *)tmp_p;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement a generic linked list. The struct for the node is
I'm trying to implement linked-lists with c struct, I use malloc to allocate a
I'm trying to implement a ring buffer with the following struct /*head, tail are
Trying to implement a search similar to here .This searches properties based on city,locality,property
Trying to implement 3-layer (not: tier, I just want to separate my project logically,
So I'm trying to implement the following; // Destructive Abstract data type ilist struct
I'm currently trying to understand how to implement a 2-dimensional array of struct in
I am trying to implement the adaptive huffman code, but while trying to build
I'm trying to implement the ringbuffer from this post https://stackoverflow.com/a/827749 and the only code
I'm trying to implement pipelining in an Operating Systems shell project by creating a

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.