473,322 Members | 1,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

deletion of mem alloated using placement new

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?
Aug 31 '07 #1
5 1376
Anonymous wrote:
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) );
}

}
};
Am i deallocating correctly?
Aug 31 '07 #2
On 2007-08-31 12:36, Anonymous wrote:
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.

--
Erik Wikström
Aug 31 '07 #3
On 31 Ago, 11:48, Gianni Mariani <gi3nos...@mariani.wswrote:
Anonymous wrote:
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) );

}
}
};
Am i deallocating correctly?
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;
}

Aug 31 '07 #4
On 31 Ago, 16:50, carvalho.mig...@gmail.com wrote:
On 31 Ago, 11:48, Gianni Mariani <gi3nos...@mariani.wswrote:
Anonymous wrote:
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) );
}
}
};
Am i deallocating correctly?

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) ?

Aug 31 '07 #5
ca*************@gmail.com 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.
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;
}
Aug 31 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Ioannis Vranos | last post by:
When we use the standard placement new operator provided in <new>, and not a definition of owr own, isn't a call to placement delete enough? Consider the code: #include <new>
6
by: Marc Mutz | last post by:
Hi, I'm in discussion with a lib vendor who uses placement new to forward construction to another ctor, like this: MyClass( ... ) { new (this) MyClass( ... ); } I asked them to use a private...
3
by: Brian Gideon | last post by:
I stumbled across something odd today about the placement of the using keyword. Section 9.3.2 of the C# v1.1 specification did not answer my question. My confusion is isolated to what happens in...
5
by: removeps-generic | last post by:
Hi. I'm using placement new to re-initialize part of an object. Is this OK? struct BaseImp { All& r_all; BaseImp(All& all) : r_all(all) }; struct Calc::Imp : public BaseImp
13
by: shsingh | last post by:
I have a class A containing some map as data variables. I creat an object of class A on heap by allocatiing memory by using "malloc". This will return me the required memory but the object is not...
5
by: Lagarde Sébastien | last post by:
Hello, I write code to debug new call with following macro: #define new (MemoryManager::Get().setOwner (__FILE__, __LINE__, _FUNCTION-), FALSE) ? NULL : new The setOwner allow to save the...
15
by: LuB | last post by:
I am constantly creating and destroying a singular object used within a class I wrote. To save a bit of time, I am considering using 'placement new'. I guess we could also debate this decision -...
9
by: karthikbalaguru | last post by:
Hi, I find that articles stating that 'placement new' constructs an object on a pre-allocated buffer and so takes less time. Actually, we have to consider the allocation of the buffer and then...
11
by: letz | last post by:
Hi, We have a class whose objects are to be allocated in shared memory. To do that a ShmManager base class is defined so that operator new and delete are redefined to allocate segments in shared...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.