Is it possible to use new to allocate adjacent blocks of memory?
Hi, I have a class which upon construction creates a specified number of objects of another class using new CObject...etc.
I was just wondering if it is possible to have each new command allocate memory adjacent to the block previously allocated. My current thinking is that the only way to do this would be to allocate a large block of memory and then allocate the objects within this large block. Just a guess though!
Thanks
Use an array or std::vector maybe?
Or allocate a single block and use placement new (which is effectively what the above does).
Or allocate a single block and use placement new (which is effectively what the above does).
Factory Pattern with Placement New (1, 2, 3) is probably what you are looking for.
You can create a chunk of memory and then use a list to keep track of what in the chunk is available. Have a list for both 'new' and 'used' memory. When an object is deleted/released back to the factory, ensure it is in the used list (to catch bad releases), deconstruct the object, and put the memory back in the free list.
When the factory is being deconstructed, make sure all the memory is in the free list. Then you can free the entire block.
EDIT: I just realized I wrote a journal post about this a lonnngggg time ago. A bit old, but you can look at some of my code here (look at entry for June 19th). I have a full listing of code in the comments of another journal, which can be found here. You might want to read this post to understand some of the design decisions. Please note that the code is VERY OLD and does not necessarily represent what I feel are good design decisions anymore. But you might be able to rip the placement new stuff out.
[Edited by - visage on December 8, 2008 5:18:47 AM]
You can create a chunk of memory and then use a list to keep track of what in the chunk is available. Have a list for both 'new' and 'used' memory. When an object is deleted/released back to the factory, ensure it is in the used list (to catch bad releases), deconstruct the object, and put the memory back in the free list.
When the factory is being deconstructed, make sure all the memory is in the free list. Then you can free the entire block.
EDIT: I just realized I wrote a journal post about this a lonnngggg time ago. A bit old, but you can look at some of my code here (look at entry for June 19th). I have a full listing of code in the comments of another journal, which can be found here. You might want to read this post to understand some of the design decisions. Please note that the code is VERY OLD and does not necessarily represent what I feel are good design decisions anymore. But you might be able to rip the placement new stuff out.
[Edited by - visage on December 8, 2008 5:18:47 AM]
Are you sure you need to be using a linked list?
I've been trying to find a good reference which explains the trade offs between different collection types, but haven't had much success (anybody know any good links?).
Anyway if you have to use a linked list, and are doing alot of insertion and deletion (otherwise why use a linked list?), then you are not going to be able to keep memory usage contiguous (in a single block), without doing alot of reallocation and copying. In that case you may aswell just use an std::vector.
I've been trying to find a good reference which explains the trade offs between different collection types, but haven't had much success (anybody know any good links?).
Anyway if you have to use a linked list, and are doing alot of insertion and deletion (otherwise why use a linked list?), then you are not going to be able to keep memory usage contiguous (in a single block), without doing alot of reallocation and copying. In that case you may aswell just use an std::vector.
Ok thanks guys, I've been looking at placement new and I now have an array of objects that I allocatoe like so:
Thing is when I do it this way it's throwing up a linker error LNK2001.
Strange because if I do it with a base type (like int) it works fine.
p.s. the CObject template class DOES have a default constructor...
//Class propertyCObject< Type >* m_Data;//...in constructorm_Data = new CObject< Type >[Size];
Thing is when I do it this way it's throwing up a linker error LNK2001.
Strange because if I do it with a base type (like int) it works fine.
p.s. the CObject template class DOES have a default constructor...
Quote:Original post by DOrmisher
Ok thanks guys, I've been looking at placement new and I now have an array of objects that I allocatoe like so://Class propertyCObject< Type >* m_Data;//...in constructorm_Data = new CObject< Type >[Size];
Thing is when I do it this way it's throwing up a linker error LNK2001.
Strange because if I do it with a base type (like int) it works fine.
p.s. the CObject template class DOES have a default constructor...
The parameter type you are using may be missing a default constructor; I assume you are creating an instance of 'Type' somewhere in CObject<Type>.
Well type is a template type. But no worries anyway the problem was with my CObject destructor...problem sorted.
Thanks
Thanks
My little test code of your problem works fine:
What are you doing that is different from that?
P.S what you mention is not placement new, just the array version of new. Placement new constructs an object at a location in memory that has been previously allocated and looks like:
/edit
oops too slow!
#include "stdafx.h"template < class T >class CObject{ T* tptr;public: CObject() : tptr(NULL) {}};template < class T >class TestClass{ CObject < T >* obs;public: TestClass () { obs = new CObject < T > [5]; } ~TestClass() { delete[] obs; }};int _tmain(int argc, _TCHAR* argv[]){ TestClass<int> tclass; return 0;}
What are you doing that is different from that?
P.S what you mention is not placement new, just the array version of new. Placement new constructs an object at a location in memory that has been previously allocated and looks like:
void* place = operator new(sizeof(Something));Something* p = new (place) Something();
/edit
oops too slow!
Quote:Original post by DOrmisher
Well type is a template type. But no worries anyway the problem was with my CObject destructor...problem sorted.
Thanks
Ya it is obviously a template type :). I just meant that you could be creating one when you created the template class, if I had of looked at the original post more carefully i would have seen that you do not, doh :)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement