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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:32:46+00:00 2026-05-15T13:32:46+00:00

Let’s say I have a program(C++, for example) that allocates multiple objects, never bigger

  • 0

Let’s say I have a program(C++, for example) that allocates multiple objects, never bigger than a given size(let’s call it MAX_OBJECT_SIZE).

I also have a region(I’ll call it a “page”) on the heap(allocated with, say, malloc(REGION_SIZE), where REGION_SIZE >= MAX_OBJECT_SIZE).
I keep reserving space in that page until the filled space equals PAGE_SIZE(or at least gets > PAGE_SIZE – MAX_OBJECT_SIZE).

Now, I want to allocate more memory. Obviously my previous “page” won’t be enough. So I have at least two options:

  1. Use realloc(page, NEW_SIZE), where NEW_SIZE > PAGE_SIZE;
  2. Allocate a new “page”(page2) and put the new object there.

If I wanted to have a custom allocate function, then:

  1. Using the first method, I’d see how much I had filled, and then put my new object there(and add the size of the object to my filled memory variable).
  2. Using the second method, I’d have a list(vector? array?) of pages, then look for the current page, and then use a method similar to 1 on the selected page.

Eventually, I’d need a method to free memory too, but I can figure out that part.

So my question is: What is the most efficient way to solve a problem like this? Is it option 1, option 2 or some other option I haven’t considered here? Is a small benchmark needed/enough to draw conclusions for real-world situations?
I understand that different operations may perform differently, but I’m looking for an overall metric.

  • 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-15T13:32:46+00:00Added an answer on May 15, 2026 at 1:32 pm

    In my experience option 2 is much easier to work with has minimal overhead. Realloc does not guarantee it will increase the size of existing memory. And in practice it almost never does. If you use it you will need to go back and remap all of the old objects. That would require that you remember where every object allocated was… That can be a ton over overhead.

    But it’s hard to qualify “most efficient” without knowing exactly what metrics you use.

    This is the memory manager I always use. It works for the entire application not just one object.

    allocs:

    for every allocation determine the size of the object allocated.

    1 look at a link list of frees for objects of that size to see if anything has been freed if so take the first free

    2 look for in a look up table and if not found

    2.1 allocate an array of N objects of the size being allocated.

    3 return the next free object of the desired size.

    3.1 if the array is full add a new page.

    N objects can be programmer tunned. If you know you have a million 16 byte objects you might want that N to be slightly higher.

    for objects over some size X, do not keep an array simply allocate a new object.

    frees:

    determine the size of the object, add it to the link list of frees.

    if the size of the object allocated is less than the size of a pointer the link list does not need to incur any memory overhead. simply use the already allocated memory to store the nodes.

    The problem with this method is memory is never returned to the operating system until the application has exited or the programmer decides to defragment the memory. defragmenting is another post. it can be done.

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

Sidebar

Related Questions

Let's say that I have an arbitrary string like `A man + a plan
Let's say I have a table that looks something like this: ------------------------------- id|column2|column3 |column4
Let's say that I'm currently designing an application where I will need to use
Let's say I have a link in a table like: <td class=ms-vb width=100%> <a
Let's say I have a class like this: public class Person { private String
Let's say I have this: public DefaultListModel model = new DefaultListModel(); how do i
Let's say I have two files.. I want to compare them side-by-side and see
Let's say I have a row: 一天吃一個蘋果 Someone enters as a query: 天蘋 Should
Let's say I have two entities: Physician Credentials And a physician can have many
Let presume we have something like this: <div1> <h1>text1</h1> <h1>text2</h1> </div1> <div2> <h1>text3</h1> </div2>

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.