deletion of mem alloated using placement new

A

Anonymous

I am writing memory allocation class policies for a template class.

I have defined one allocated like this:

template <class T>
struct myMallocAllocator
{
static T* Allocate()
{
void * ptr = calloc(1, sizeof(T));
if (ptr)
return new(ptr) T ;
else
return 0 ;
}

static void DeAllocate(T *ptr)
{
if(ptr);
ptr->~T() ;
}
};


Am i deallocating correctly?
 
G

Gianni Mariani

Anonymous said:
I am writing memory allocation class policies for a template class.

I have defined one allocated like this:

template <class T>
struct myMallocAllocator
{
static T* Allocate()
{
void * ptr = calloc(1, sizeof(T));
if (ptr)
return new(ptr) T ;
else
return 0 ;

The C++ thing is to throw std::bad_alloc and not return 0.

}

static void DeAllocate(T *ptr)
{
if(ptr);
ptr->~T() ;

if (ptr) {
ptr->~T();
free( static_cast<void*>(ptr) );
}
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

I am writing memory allocation class policies for a template class.

I have defined one allocated like this:

template <class T>
struct myMallocAllocator
{
static T* Allocate()
{
void * ptr = calloc(1, sizeof(T));
if (ptr)
return new(ptr) T ;
else
return 0 ;
}

static void DeAllocate(T *ptr)
{
if(ptr);
ptr->~T() ;
}
};


Am i deallocating correctly?

You forgot to deallocate, since you used calloc to allocate you need to
use free to deallocate the memory, i.e. put 'free(ptr);' last in your
DeAllocate() function.
 
C

carvalho.miguel

The C++ thing is to throw std::bad_alloc and not return 0.



if (ptr) {
ptr->~T();
free( static_cast<void*>(ptr) );

}

whats with the destructor calling? when you delete a pointer the
destructor is automatically called.
also is perfect valid to delete a NULL pointer, so no need to check if
it exists.

also why use malloc/calloc and free? why not new/delete? i cant see a
valid reason in this example to not use the standard c++ allocate/
deallocate operators.
so simple, no need to complicate it:

template <class T>
class Allocator
{
public:
static T* Allocate()
{
return new T;
}

static void Deallocate( T* t )
{
delete t;
}
};

class Test
{
public:
Test()
{
std::cout << "Test::Test()" << std::endl;
}

~Test()
{
std::cout << "Test::~Test()" << std::endl;
}
};

int main( int argc, char** argv )
{
Test *t = Allocator<Test>::Allocate();
Allocator<Test>::Deallocate( t );

return 0;
}
 
C

carvalho.miguel

whats with the destructor calling? when you delete a pointer the
destructor is automatically called.
also is perfect valid to delete a NULL pointer, so no need to check if
it exists.

also why use malloc/calloc and free? why not new/delete? i cant see a
valid reason in this example to not use the standard c++ allocate/
deallocate operators.
so simple, no need to complicate it:

template <class T>
class Allocator
{
public:
static T* Allocate()
{
return new T;
}

static void Deallocate( T* t )
{
delete t;
}

};

class Test
{
public:
Test()
{
std::cout << "Test::Test()" << std::endl;
}

~Test()
{
std::cout << "Test::~Test()" << std::endl;
}

};

int main( int argc, char** argv )
{
Test *t = Allocator<Test>::Allocate();
Allocator<Test>::Deallocate( t );

return 0;

}

also, why use placement new if you're always reserving a buffer of
size 1 x sizeof(T) ?
 
G

Gianni Mariani

(e-mail address removed) wrote:
....

I think the OP just wants to know if what was posted would work. It had
problems and I corrected them. As for why the OP wants to use
calloc/free, I don't know but I can come up with at least 2 reasons why
it might be interesting to do so.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top