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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:06:03+00:00 2026-06-12T10:06:03+00:00

I’ve been given a custom allocator that implements it’s own allocation policies, its own

  • 0

I’ve been given a custom allocator that implements it’s own allocation policies, its own malloc/free, etc. Now, I’ve been asked to use this custom allocator with a STL container (being it a vector or some other). I’ve created a class, say my_stdAllocator, that is an interface compliant to the ISO C++ standard. through this class I call my allocator’s methods. For example:

template <class T>
class my_stdAllocator {

    // ...other required stuff...

    // allocate 'num' elements of type T
    pointer allocate(size_type num, const_pointer = 0) {
        return static_cast<T*>(MYAllocator::instance()->malloc(num));
    }

    // deallocate memory at 'p'
    void deallocate(pointer p, size_type num=0) { 
        MYAllocator::instance()->free(p);
    }

    // initialize allocated memory at 'p' with value 'value'
    void construct(pointer p, const T& value) {   
        ::new ((void*)p) T(value);  
    }


    // destroy elements of initialized memory at p
    void destroy(pointer p) {
        p->~T();
    }

    // ...other required stuff...

} // end my_stdAllocator

The custom allocator normally works like a charm: it has been extensively tested, it definitely improves performances, limits fragmentation, etc. When I use it as the allocator for a stl container, (say, a vector) it has this weird behaviour in that it sometimes works properly, while it crashes with a segfault error some other times.

To give an example, it properly allocates and frees with a vector of char:

typedef char TP;

int main(int argc, char* argv[]) { 

std::vector<TP, my_stdAllocator<TP> > vec;

std::string s ("Whatever string, no matter how long...");

std::string::iterator it;
for (it=s.begin(); it<s.end(); ++it)
    vec.push_back(*it);

...

It is ok when pushing “by hand” numbers inside the vector

typedef double TP;

int main(int argc, char* argv[]) { 

std::vector<TP, my_stdAllocator<TP> > vec;

// "manual" push_back
vec.push_back(3.2);
vec.push_back(6.4);
vec.push_back(9.6);
vec.push_back(12.8);
vec.push_back(15.1);
vec.push_back(18.3);
vec.push_back(21.5);

...

It halts with a Segmentation fault when inserting elements through a loop:

typedef int TP;

int main(int argc, char* argv[]) { 

std::vector<TP, ff_stdAllocatorInst<TP> > vec;

for(unsigned int i=0; i<size; ++i)
    vec.push_back( (TP) i );

...

It works like a charm when reserving space for at least a certain number of elements:

typedef int TP;

int main(int argc, char* argv[]) { 

std::vector<TP, ff_stdAllocatorInst<TP> > vec;

vec.reserve(size);
for(unsigned int i=0; i<size+150; ++i)
    vec.push_back( (TP) i );

...

Notice that, the segfault mentioned above doesn’t happen when using a placement new like this one:

void *p = MYAllocator::instance()->malloc(size);
std::vector<TP> *vec = new (p) std::vector<TP>
for(unsigned int i=0; i<size; ++i)
    vec->push_back( (TP) i );

...    

As I’ve said already, the custom allocator has been tested and works fine. My class is a simple interface between C++ standard and the custom allocator. I’ve tried to debug it wth gdb but it didn’t help: the underlying allocator is ok, there must be some mistakes in my code but I cannot understand what is wrong!

  • 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-12T10:06:04+00:00Added an answer on June 12, 2026 at 10:06 am

    In your call to your custom allocator’s malloc function, you need to multiply by the size of the object you’re allocating:

    pointer allocate(size_type num, const_pointer = 0) {
        return static_cast<T*>(MYAllocator::instance()->malloc(num*sizeof(T)));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I know there's a lot of other questions out there that deal with this

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.