MemoryManager<Test> mem;
Test* test = mem.allocate(10);
test[2] = new Test;
This didn't work. I have no operator= defined for Test, but how do I go about actually initializing objects in this space?
EDIT:
This works:
1 2 3 4
MemoryManager<Test> mem;
Test* test = mem.allocate(10);
Test test2;
test[2] = test2;
This is how I imagine this should work. I get the space allocated, construct objects however I want, then put them this space. But, this seems like I'm creating an object on the stack, then placing a copy of it on the heap. Is this normal? I've never dabbled with memory management in this manner before, so I'm still a little lost, and it's hard to find resources on this. So far I've only really managed to find an article by IBM on memory management systems.
using an allocator, which is what you do in a vector, shared_ptr, function, or another allocator-aware class is a whole different story from writing an allocator that all existing allocator-aware class can use.
In a vector, you would call the function construct(), provided by allocator_traits (as of C++11) or directly by the allocator. Assignment to uninitialized memory is only valid for trivially-copyable types.
Isn't it simply like... return (0U - 1U) / sizeof(T);
which is also like return -1 / sizeof(T);
or return 0xFFFFFFFF / sizeof(T);
or return UINT_MAX / sizeof(T);
??
I have it changed to what cire has posted, as I think it's much more readable. From The Mallocator though, the author said that his way is implementation dependent, though I don't see how this way would be otherwise?
@kbw,
Yea I know, I'm missing several things for that. I'm currently just trying to get the bare essentials working. As this is all new grounds for me I'm trying to work on one thing at a time.