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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:25:28+00:00 2026-06-07T11:25:28+00:00

Is there a malloc/free based allocator in the STL? If not, does anyone know

  • 0

Is there a malloc/free based allocator in the STL? If not, does anyone know of a simple copy/paste one? I need it for a map that must not call new/delete.

  • 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-07T11:25:30+00:00Added an answer on June 7, 2026 at 11:25 am

    First, I’d note that changing the allocator for the map itself won’t change the allocation used by the objects stored in the map. For example, if you do something like:

    std::map<std::string, int, my_allocator<std::pair<const std::string, int> > m;
    

    The map itself will allocate memory using the specified allocator, but when the std::strings in the map allocate memory, they’ll still use the default allocator (which will use new and delete. So, if you need to avoid new and delete in general, you have to ensure that not only the map itself uses the right allocator, but that any objects it stores do the same (I know that’s probably stating the obvious, but I’ve overlooked it, so maybe it’s worth mentioning).

    With that proviso, on with the code:

    #ifndef ALLOCATOR_H_INC_
    #define ALLOCATOR_H_INC_
    
    #include <stdlib.h>
    #include <new>
    #include <limits>
    
    namespace JVC {
    template <class T> 
    struct allocator {
        typedef size_t size_type;
        typedef ptrdiff_t difference_type;
        typedef T* pointer;
        typedef const T* const_pointer;
        typedef T& reference;
        typedef const T& const_reference;
        typedef T value_type;
    
        template <class U> struct rebind { typedef allocator<U> other; };
        allocator() throw() {}
        allocator(const allocator&) throw() {}
    
        template <class U> allocator(const allocator<U>&) throw(){}
    
        ~allocator() throw() {}
    
        pointer address(reference x) const { return &x; }
        const_pointer address(const_reference x) const { return &x; }
    
        pointer allocate(size_type s, void const * = 0) {
            if (0 == s)
                return NULL;
            pointer temp = (pointer)malloc(s * sizeof(T)); 
            if (temp == NULL)
                throw std::bad_alloc();
            return temp;
        }
    
        void deallocate(pointer p, size_type) {
            free(p);
        }
    
        size_type max_size() const throw() { 
            return std::numeric_limits<size_t>::max() / sizeof(T); 
        }
    
        void construct(pointer p, const T& val) {
            new((void *)p) T(val);
        }
    
        void destroy(pointer p) {
            p->~T();
        }
    };
    }
    
    #endif
    

    And, a little test code:

    #include <map>
    #include <vector>
    #include <iostream>
    #include <string>
    #include <iterator>
    #include "allocator.h"
    
    // Technically this isn't allowed, but it's only demo code, so we'll live with it.
    namespace std { 
    std::ostream &operator<<(std::ostream &os, std::pair<std::string, int> const &c) { 
        return os << c.first << ": " << c.second;
    }
    }
    
    int main() { 
        std::map<std::string, int, std::less<std::string>, 
                 JVC::allocator<std::pair<const std::string, int> > > stuff;
    
        stuff["string 1"] = 1;
        stuff["string 2"] = 2;
        stuff["string 3"] = 3;
    
        std::copy(stuff.begin(), stuff.end(), 
            std::ostream_iterator<std::pair<std::string, int> >(std::cout, "\n"));
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently studying the malloc implementation for my homework. I know there exists some
I've written a c/c++ memory manager for heap allocations (overloaded new/delete and malloc/realloc/free, based
I am wondering if there is a cross-platform allocator that is one step lower
I'm not familiar with how the Linux heap is allocated. I'm calling malloc()/free() many
While there are lots of different sophisticated implementations of malloc / free for C/C++,
There is a moment in my app, that I need to force to show
There are two table s : one is the master and one the detail
(I've edited this question to avoid distractions. There is one core question which would
I have to implement an optimized version of malloc/realloc/free (tailored for my particular application).
I have a programming project with highly intensive use of malloc/free functions. It has

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.