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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:55:09+00:00 2026-05-13T17:55:09+00:00

I am working with a memory manager that, on occasion, wants to defragment memory.

  • 0

I am working with a memory manager that, on occasion, wants to defragment memory. Basically, I will go through a list of objects allocated by the memory manager and relocate them:

class A {
  SomeClass* data; // This member is allocated by the special manager
};

for(... each instance of A ...) 
    a.data = memory_manager.relocate(a.data);

memory_manager.relocate() will memcpy() the contents of data to a new location, and return the pointer.

Although it’s generally not idiomatic to memcpy() C++ classes, it seems to be a useful solution in this case, considering that I control the implementation of the (few) classes that will be used with the memory manager.

The problem is that one of those classes uses std::map, which is an opaque class as far as I am concerned. I certainly don’t imagine I can memcpy() it. I may not be able to use std::map in any case. For all I know it could allocate several pieces of memory.

The best workaround I can think of is simple enough. Due to the fact that the fragmented memory manager will put new allocations at more beneficial locations, all I need to do is allocate it anew and then delete the old:

for(... each instance of A ...) {
    stl::map<something>* tmp = a.the_map;
    a.the_map = new stl::map<something>(tmp);
    delete tmp;
}

In any case, this lead me to wonder:

Does C++ have semantics or idioms to move/copy an object into a specific memory location?

Is it possible to move the contents of an stl container to a specific memory location?

Edit: Although I didn’t point it out, I would obviously pass an allocator parameter to std::map. Based on the informative answers I got, I realize the workaround I posted in my initial question would probably be the only way to reduce fragmentation. By using the map’s copy constructor (and the allocator template parameter), all memory used by the map would be properly re-allocated.

As a comment pointed out, this is mostly a theoretical problem. Memory fragmentation is rarely something to worry about.

  • 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-13T17:55:10+00:00Added an answer on May 13, 2026 at 5:55 pm

    Everytime you insert a new key,value pair the map will allocate a node to store it. The details of how this allocation takes place are determined by the allocator that the map uses.

    By default when you create a map as in std::map<K,V> the default allocator is used, which creates nodes on the heap (i.e., with new/delete).

    You don’t want that, so you’ll have to create a custom allocator class that creates nodes as dictated by your memory manager.

    Creating an allocator class is not trivial. This code shows how it can be done, you’ll have to adapt it to your own needs.

    Once you have your allocator class (let’s say you call it MemManagerAllocator) you’ll have to define your map as std::map<K, V, MemManagerAllocator> and then use it like you would use a regular map.

    Personally, I would need to have a really bad problem of memory fragmentation to go into all that trouble.

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

Sidebar

Related Questions

I am working on a memory matching game. I have some code that checks
I am working on a project that is both memory and computationally intensive. A
I working on a project in which I have to simulate a memory manager
Task Manager says that IE is using over 500MB of private working set. Both
I am working on a background program that will be running for a long
Possible Duplicate: Is there any working memory profiler for Python3 I have some script
I'm working on a memory game for Android and I'm having a problem. When
I am working on a memory matching game. Right now, when the user clicks
I'm working on a memory matching game. Right now, the game is working fine.
I'm working on a memory tracking library where we use mprotect to remove access

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.