Question With Memory Allocation

closed account (N36fSL3A)
Hi, I've read a while ago that memory allocation with new for each and every object was inefficient. Knowing me, I obviously read more on the matter.

I think it was Framework who said this, and he stated what was said above, then he said that his framework allocated large blocks of memory (~1 mb of memory), and then subdivided the memory on request.

How can I do this?
Last edited on
That's not necessarily true. If you have a specific memory issue to handle, then you can create your own allocator by deriving from std::allocator
http://www.cplusplus.com/reference/memory/allocator/

The C and C++ standard libraries have done that for ages -- get a large chunk of memory from the system and then dole out pieces to your program.
closed account (N36fSL3A)
Can you elaborate?
That's not necessarily true. If you have a specific memory issue to handle, then you can create your own allocator by deriving from std::allocator

I was told that's a bad idea. The lack of virtual destructor hints that std::allocator isn't meant to be inherited.

http://www.codeproject.com/Articles/4795/C-Standard-Allocator-An-Introduction-and-Implement
http://www.drdobbs.com/the-standard-librarian-what-are-allocato/184403759

You should use a ready-made allocator and if you feel like learning how it works, just read the code.
http://warp.povusers.org/FSBAllocator/
closed account (N36fSL3A)
Have you ever used it? Is it free for commercial use?
The lack of virtual destructor hints that std::allocator isn't meant to be inherited.

It isn't, but since it is used with templates it doesn't matter -- the templated class will create and destroy its allocator based on the allocator's declared type. No polymorphism involved.
closed account (N36fSL3A)
So I should or shouldn't use std::allocator?
So long as your allocator implements the same methods as std::allocator, it doesn't matter.
Topic archived. No new replies allowed.