Aug 8, 2012 at 7:30pm UTC
I want to make my own allocating function which call the constructor for each object but I have problems here I is my code:
1 2 3 4 5 6 7 8
template <class T>
T* MyAllocHeap(unsigned long size, const T& ObjTmpl, bool bSave) {
T* Obj=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size*sizeof (T));
if (bSave)
for (unsigned long cr(0); cr<size; ++cr)
memcpy(&Obj[cr], &ObjTmpl, sizeof (T));
return Obj;
}
I realize this code isn't very effective cause I just copy one static object and if in this object is allocated memory there'll be leaks:
1 2 3 4 5
class My{
My() { Obj=new classEx; }
classEx* Obj;
} ;
MyAllocHeap<My>(1, My(), 1);
My function need to be something like this but It didn't compile:
1 2 3 4 5 6 7 8
template <class T>
T* MyAllocHeap(unsigned long size, T (*ObjTmplFunc)(...), bool bSave, ... /*funct argum*/ )) {
T* Obj=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size*sizeof (T));
if (bSave)
for (unsigned long cr(0); cr<size; ++cr)
Obj[cr].ObjTmplFunc(args), &ObjTmpl, sizeof (T));
return Obj;
}
[/code]
Can you help me with function like this but working with c++ ?
Last edited on Aug 8, 2012 at 7:31pm UTC
Aug 8, 2012 at 10:38pm UTC
operator new ( size * sizeof ( T ) );
To allocate memory without calling the constructor of a class.
1 2 3 4 5 6
void * ptr;
class MyObject;
new ( ptr ) MyObject();
Constructors MyObject at ptr.
Also you shouldn't "save" a class by using memcpy, that would create a shallow copy, you should instead use the copy constructor.
Last edited on Aug 8, 2012 at 10:40pm UTC
Aug 9, 2012 at 6:59am UTC
Maybe you don't understand me - I want to make, my allocate function, to call a specific constructor function, passed with the arguments, on every object.
Not using the new operator.
EDIT: Actually I want to allocate the memory for my objects and then call them constructor.
Last edited on Aug 9, 2012 at 7:26am UTC
Aug 9, 2012 at 8:17am UTC
> Actually I want to allocate the memory for my objects and then call them constructor.
This would be the general idea:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include <memory>
#include <cstdlib>
#include <new>
#include <iostream>
template < typename T > void delete_object( T* p )
{
p->T::~T() ; // destroy the object
std::free(p) ; // and free its memory
}
template < typename T, typename ... CONSTRUCTOR_ARGS >
std::shared_ptr<T> make_object( CONSTRUCTOR_ARGS... constructor_args )
{
void * p = std::malloc( sizeof (T) ) ; // allocate memory
// construct with a placement new
return std::shared_ptr<T>( ::new (p) T( constructor_args... ), delete_object<T> ) ;
}
struct A
{
A() { std::cout << "A::default constructor\n" ; }
explicit A( int , int =6 ) { std::cout << "A::A( int, int )\n" ; }
A( const A& ) { std::cout << "A::copy constructor\n" ; }
~A() { std::cout << "A::destructor\n" ; }
};
struct B
{
B( const char *, double ) { std::cout << "B::B( const char*, double )\n" ; }
~B() { std::cout << "B::destructor\n" ; }
};
int main()
{
std::shared_ptr<A> a1 = make_object<A>() ;
std::shared_ptr<A> a2 = make_object<A>(12) ;
std::shared_ptr<A> a3 = make_object<A>(12, 30 ) ;
std::shared_ptr<A> a4 = make_object<A>(*a1) ;
std::shared_ptr<B> b = make_object<B>( "hello world!" , 1234.567 ) ;
}
In practice, you would want to write an allocator of your own and then just use
std::allocate_shared<>()
http://en.cppreference.com/w/cpp/memory/shared_ptr/allocate_shared
Last edited on Aug 9, 2012 at 8:25am UTC
Aug 9, 2012 at 9:57am UTC
Thanks for giving me that new information for me. I was never heard about this before but I'll read some articles. I'll mark this thread as a solved now.