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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:15:22+00:00 2026-05-14T04:15:22+00:00

So I am trying to create a Memory Management System. In order to do

  • 0

So I am trying to create a Memory Management System. In order to do this I have a set amount of space (allocated by malloc) and then I have a function myMalloc which will essentially return a pointer to the space allocated. Since we will then try and free it, we are trying to set a header of the allocated space to be the size of the allocated space, using memset.

memset(memPtr,sizeBytes,sizeof(int));

We then need to be able to read this so we can see the size of it. We are attempting to do this by using memcpy and getting the first sizeof(int) bytes into a variable. For testing purposes we are just trying to do memset and then immediately get the size back. I’ve included the entire method below so that you can see all declarations. Any help would be greatly appreciated! Thanks!

void* FirstFit::memMalloc(int sizeBytes){

node* listPtr = freelist;
void* memPtr;   

// Cycle through each node in freelist
while(listPtr != NULL)
{

    if(listPtr->size >= sizeBytes) 
    {
        // We found our space
        // This is where the new memory allocation begins
        memPtr = listPtr->head;
        memset(memPtr,sizeBytes,sizeof(int));

        void *size;
        memcpy(size, memPtr, sizeof(int));
        // Now let's shrink freelist
        listPtr->size = listPtr->size - sizeBytes;

        int *temp = (int*)listPtr->head + (sizeBytes*sizeof(int));  
        listPtr->head = (int*) temp;
        return memPtr;
    }

    listPtr = listPtr->next;
}

::Edit::
Sorry! When running this, we keep getting a seg fault when attempting to run the memcpy line. We have been playing with different ideas for the past hour or so and honestly just have no idea where the error is occurring.

::Edit2::
I also posted this as a comment, but figured I’d put it here as well, so it was easier to find…

Our problem is that we have an allocated space that we are allowed to work with, specified by one malloc call for 128MB. We can only use this, so we can’t initialize size to anything using malloc. I guess, is there a way to do this without initializing size. If not, is there anyway to get the int that the header is set to without using memcpy.

  • 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-14T04:15:22+00:00Added an answer on May 14, 2026 at 4:15 am

    You code has numerous bugs in it – rather than go through them one-by-one, I’ll give you a commented version of what it should look like:

    void* FirstFit::memMalloc(size_t sizeBytes)  // size_t is the appropriate type for memory sizes
    {
        node* listPtr = freelist;
        void* memPtr;   
        // The actual allocation needs to be bigger, to have space to hold the size itself.
        size_t allocSize = sizeBytes + sizeof allocSize;
    
        // Test to make sure that allocSize didn't wrap around to zero
        if (allocSize < sizeBytes)
        {
            return NULL;
        }
    
        // Cycle through each node in freelist
        while(listPtr != NULL)
        {
    
            if(listPtr->size >= allocSize) 
            {
                // We found our space
                // This is where the new memory allocation begins
                memPtr = listPtr->head;
    
                // Copy the size to the start of the memory region
                memcpy(memPtr, &allocSize, sizeof allocSize);
    
                // Increment the pointer to be returned past the size
                char *tempPtr = (char *)memPtr;
                memPtr = (void *)(tempPtr + sizeof allocSize);
    
                // Shrink the block
                listPtr->size -= allocSize;
                tempPtr = (char *)listPtr->head;
                listPtr->head = (void *)(tempPtr + allocSize);
    
                // TODO: If the block is now zero-sized, remove it from the linked list
    
                return memPtr;
            }
    
        listPtr = listPtr->next;
        }
    
        /* No space */
        return NULL;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Heyy Everybody! I am trying to create a memory management system, so that a
I'm trying to understand memory management on iOS. I created this interface: @interface Player
I'm trying to create a memory game - where I have 5x5 images on
I am trying to create a memory game in Java. Something like this, but
I'm trying to create a contiguous block of memory in one function call that
I am trying to create an offline registry in memory using the offreg.dll provided
I'm trying to avoid the memory leak in PHP. When I create a object
I am trying to create an offline registry in memory using the offreg.dll provided
I'm trying to create integration tests using hsqldb in an in memory mode. At
I have a program that simulates best-fit memory management. Basically, while there are available

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.