C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

placement new , is this acceptable?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
lallous
Guest





PostPosted: Tue Feb 10, 2004 10:37 am    Post subject: placement new , is this acceptable? Reply with quote



class MyClass
{
static void *Allocate(int size);
static void Free(void *mem);
static MyClass *CreateInstance();
void DestroyInstance();
};

MyClass *MyClass::CreateInstance()
{
void * p = Allocate(sizeof(MyClass));

return new (p) MyClass;
}

void MyClass::DestroyInstance()
{
MyClass *i = this;
i->~MyClass();
Free(i);
}


Back to top
Jonathan Turkanis
Guest





PostPosted: Tue Feb 10, 2004 10:52 am    Post subject: Re: placement new , is this acceptable? Reply with quote




"lallous" <lallous (AT) lgwm (DOT) org> wrote

Quote:
class MyClass
{
static void *Allocate(int size);
static void Free(void *mem);
static MyClass *CreateInstance();
void DestroyInstance();
};

MyClass *MyClass::CreateInstance()
{
void * p = Allocate(sizeof(MyClass));

what if p == 0?

Quote:

return new (p) MyClass;
}

void MyClass::DestroyInstance()
{
MyClass *i = this;
i->~MyClass();
Free(i);
}


Who calls CreateInstace and DestroyInstance? Both are private.

This can be made to work, but why? The standard way to provide custom
allocation facilities for a class is to provide custom member
operators new and delete. Using a factory function instead of a public
constructor is appropriate sometimes, but is usually independent of
the decision to provide custom allocation. Also, why make the
constructor private?

Jonathan



Back to top
lallous
Guest





PostPosted: Tue Feb 10, 2004 11:54 am    Post subject: Re: placement new , is this acceptable? Reply with quote



"Jonathan Turkanis" <technews (AT) kangaroologic (DOT) com> wrote

Quote:

"lallous" <lallous (AT) lgwm (DOT) org> wrote in message
news:c0ackn$14hqr1$1 (AT) ID-161723 (DOT) news.uni-berlin.de...
class MyClass
{
static void *Allocate(int size);
static void Free(void *mem);
static MyClass *CreateInstance();
void DestroyInstance();
};

MyClass *MyClass::CreateInstance()
{
void * p = Allocate(sizeof(MyClass));

what if p == 0?


return new (p) MyClass;
}

void MyClass::DestroyInstance()
{
MyClass *i = this;
i->~MyClass();
Free(i);
}


Who calls CreateInstace and DestroyInstance? Both are private.

This can be made to work, but why? The standard way to provide custom
allocation facilities for a class is to provide custom member
operators new and delete. Using a factory function instead of a public
constructor is appropriate sometimes, but is usually independent of
the decision to provide custom allocation. Also, why make the
constructor private?

Jonathan


Hello


Consider all method public, and an instance is created as:

MyClass *a = MyClass::CreateInstance();
a->DestroyInstance();

What is a factory function? can you provide links?

--
Elias



Back to top
Claudio Puviani
Guest





PostPosted: Tue Feb 10, 2004 3:11 pm    Post subject: Re: placement new , is this acceptable? Reply with quote

"lallous" <lallous (AT) lgwm (DOT) org> wrote
Quote:
class MyClass
{
static void *Allocate(int size);
static void Free(void *mem);
static MyClass *CreateInstance();
void DestroyInstance();
};

MyClass *MyClass::CreateInstance()
{
void * p = Allocate(sizeof(MyClass));

return new (p) MyClass;
}

void MyClass::DestroyInstance()
{
MyClass *i = this;
i->~MyClass();
Free(i);
}

Aside from the fact that this is an overly convoluted way to do things, your
approach doesn't support inheritance. How do you allocate/deallocate an instance
of a class derived from MyClass?

Claudio Puviani



Back to top
lallous
Guest





PostPosted: Tue Feb 10, 2004 3:23 pm    Post subject: Re: placement new , is this acceptable? Reply with quote

"Claudio Puviani" <puviani (AT) hotmail (DOT) com> wrote

Quote:
"lallous" <lallous (AT) lgwm (DOT) org> wrote
class MyClass
{
public:
static void *Allocate(int size);
static void Free(void *mem);
static MyClass *CreateInstance();
void DestroyInstance();
};

MyClass *MyClass::CreateInstance()
{
void * p = Allocate(sizeof(MyClass));

return new (p) MyClass;
}

void MyClass::DestroyInstance()
{
MyClass *i = this;
i->~MyClass();
Free(i);
}

Aside from the fact that this is an overly convoluted way to do things,
your
approach doesn't support inheritance. How do you allocate/deallocate an
instance
of a class derived from MyClass?

Claudio Puviani


My question is whether the method used to create/destroy an instance is
acceptable?

For this design, no inheritance is planned.

--
Elias



Back to top
puppet_sock@hotmail.com
Guest





PostPosted: Tue Feb 10, 2004 6:26 pm    Post subject: Re: placement new , is this acceptable? Reply with quote

"lallous" <lallous (AT) lgwm (DOT) org> wrote

[snip]
Quote:
What is a factory function? can you provide links?

A factory function is exactly the kind of function you had
in your example, one that returns a pointer to a brand new
instance of the class. It gets its name because it is often
used in a thing called a factory. The idea is, you put some
overhead into each of several classes, then register those
classes with the factory. Then when you want an instance
of that class, you can request one from the factory. There
are many variations on this: For example all the entries
could be in a single inheritance heirarchy so that they
all come back as instances of the base type. Or you may
need to provide the factory with an initial instance of
each registered class. Or you may register the class along
with the name to be used to request new instances. Or a
factory may be used to create a set of instances of a class,
and thus not reallocate the memory, but only recycle it.
Lots of fun.
Socks

Back to top
Claudio Puviani
Guest





PostPosted: Tue Feb 10, 2004 7:49 pm    Post subject: Re: placement new , is this acceptable? Reply with quote

"lallous" <lallous (AT) lgwm (DOT) org> wrote
Quote:
"Claudio Puviani" <puviani (AT) hotmail (DOT) com> wrote

Aside from the fact that this is an overly convoluted way to do things,
[...]

My question is whether the method used to create/destroy an instance is
acceptable?

What part of "overly convoluted" doesn't answer your question? I suppose one
could get philosophical and ask "acceptable by who's standard?", but in
realistic terms, no, it's not acceptable. There are no benefits whatsoever to
your approach, but plenty of limitations. It probably served its purpose in
giving you a working example of placement new and calling destructors directly,
but I seriously recommend never doing that in real code.

For some proper ways to construct object factories, I recommend Alexandrescu's
book (which someone may have already done):
http://search.barnesandnoble.com/textbooks/booksearch/isbnInquiry.asp?isbn=0201704315&TXT=Y&itm=1

Claudio Puviani




Back to top
Jonathan Turkanis
Guest





PostPosted: Wed Feb 11, 2004 1:38 am    Post subject: Re: placement new , is this acceptable? Reply with quote


"Jonathan Turkanis" <technews (AT) kangaroologic (DOT) com> wrote

Quote:


the decision to provide custom allocation. Also, why make the
constructor private?


I think I meant destructor here.

Jonathan



Back to top
lallous
Guest





PostPosted: Wed Feb 11, 2004 12:02 pm    Post subject: Re: placement new , is this acceptable? Reply with quote

"Claudio Puviani" <puviani (AT) hotmail (DOT) com> wrote

Quote:
"lallous" <lallous (AT) lgwm (DOT) org> wrote
"Claudio Puviani" <puviani (AT) hotmail (DOT) com> wrote

Aside from the fact that this is an overly convoluted way to do
things,
[...]

My question is whether the method used to create/destroy an instance is
acceptable?

What part of "overly convoluted" doesn't answer your question? I suppose
one
could get philosophical and ask "acceptable by who's standard?", but in

Is the usage of placement new and calling the destructor like that a correct
one C++ syntax wise?

I heard there is "overhead" or extra bytes that needs to be allocated in
addition to the size of the class?

[snip ...book recomendation...]

--
Elias



Back to top
Claudio Puviani
Guest





PostPosted: Wed Feb 11, 2004 1:51 pm    Post subject: Re: placement new , is this acceptable? Reply with quote

"lallous" <lallous (AT) lgwm (DOT) org> wrote
Quote:
Is the usage of placement new and calling the destructor like that a correct
one C++ syntax wise?

Syntactically, it works, though your compiler could have told you that much.

Quote:
I heard there is "overhead" or extra bytes that needs to be allocated in
addition to the size of the class?

You get overhead -- and typically much more than just one byte -- any time you
allocate using new or one of the malloc functions. Some replacement libraries,
like Hoard or SmartHeap, reduce this, there's always some amount of overhead.
You normally shouldn't have to worry about this, but if you ever have to
allocate tens or hundreds of thousands of objects, you might want to consider
specialized allocators.

Claudio Puviani



Back to top
Steve Dubak
Guest





PostPosted: Sat Mar 06, 2004 3:53 am    Post subject: Re: placement new , is this acceptable? Reply with quote

Claudio Puviani wrote:
Quote:
"lallous" <lallous (AT) lgwm (DOT) org> wrote

Is the usage of placement new and calling the destructor like that a correct
one C++ syntax wise?


Syntactically, it works, though your compiler could have told you that much.


I heard there is "overhead" or extra bytes that needs to be allocated in
addition to the size of the class?


You get overhead -- and typically much more than just one byte -- any time you
allocate using new or one of the malloc functions. Some replacement libraries,
like Hoard or SmartHeap, reduce this, there's always some amount of overhead.
You normally shouldn't have to worry about this, but if you ever have to
allocate tens or hundreds of thousands of objects, you might want to consider
specialized allocators.


Some specialized allocators utilize more memory overhead than others,
so if you're allocating many small objects, watch your memory
consumption. Hoard is pretty good with memory, much better than
smartheap. But alas, you pay for that memory conservation in speed.
Smartheap is much faster than hoard. And ESA from cherrystone software
labs is much faster than smartheap while using less memory than
smartheap. So if you're going to pick a specialized allocator, choose
carefully, benchmark, and watch the memory consumption.


Quote:

Claudio Puviani





Steve Dubak
[email]steve (AT) barragesystems (DOT) com[/email]



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.