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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:10:17+00:00 2026-05-18T03:10:17+00:00

I need to create a custom allocator for std:: objects (particularly and initially for

  • 0

I need to create a custom allocator for std:: objects (particularly and initially for std::vector) but it might eventually come to use others

The reason I need to create a custom allocator is that I need to track allocated (heap & stack) resources by individual components of the application (this is an inherent feature of the application). I will need the custom allocator to monitor the heap portion of the resources, so it is essential that I’m able to pass to the std::vector constructor something like

trackerId idToTrackUsage;
myAlloca<int> allocator(idToTrackUsage);
vector<int> Foo( allocator );

However, after reading a bit I found this little bomb about the STL / C++ standard (see references) saying that all allocator instances of a given type should be equivalent (that is that == should return true for any two instances) and, most terminal; any allocator should be able to deallocate memory allocated by any other instance (that is, without having a way to know what that other instance might be). In short, allocators cannot have state.

So I’m trying to find the best way around this. Any clever ideas? I really really REALLY don’t want to have to keep a custom version of std::vector around.

EDIT: i read about scoped allocators for c++0x on http://www2.research.att.com/~bs/C++0xFAQ.html#scoped-allocator but i couldn’t really get far into understanding how this applies to my problem. If anyone thinks c++0x alleviates this problem, please comment

References:

Allocator C++ article in Wikipedia

Some random further reading courtesy of Google

  • 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-18T03:10:18+00:00Added an answer on May 18, 2026 at 3:10 am

    Aside from the obvious answer (“if you violate any requirement, that’s undefined behavior, good night and thanks for playing”), I imagine the worst that would likely happen, is that the vector implementation can rely on the requirement that “all instances of the allocator class are interchangeable” in the obvious way:

    vector(const Allocator &youralloc = Allocator()) {
        const Allocator hawhaw;
        // use hawhaw and ignore youralloc. 
        // They're interchangeable, remember?
    }
    

    Looking at the source, GCC’s vector implementation (which I think is based eventually on SGI’s original STL implementation) does sort-of store a copy of the allocator object passed into that constructor, so there’s some hope that this won’t happen.

    I’d say try it and see, and document what you’ve done very carefully, so that anyone attempting to use your code on an implementation that you haven’t checked, knows what’s going on. Implementers are encouraged in the standard to relax the restrictions on allocators, so it would be a dirty trick to make it look as though they’re relaxed when really they aren’t. Which doesn’t mean it won’t happen.

    If you’re really lucky, there’s some documentation for your container implementation that talks about allocators.

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

Sidebar

Related Questions

I need to create a custom json for the jit library. Should I use
I need create custom tabbar with super class UITabBarController. But i don,t know how
I need to create custom events in a javascript NameSpace, but i dont know
I need to create a custom NSFormatter for some fields but in case of
I need create custom dialog and put JPanel into it. Is it possible?
Need to create a custom DNS name server using C which will check against
I need to create a custom user interface using extJS/JSP which will allow me
I need to create a custom component which consist of a drop down box,
I need to create a custom view, which extends RelativeLayout and simply needs to
I need to create a custom constraint annotation which can access the value of

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.