473,511 Members | 9,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pre-Allocate a General Class Instance

In my project, there is a class "Father",
from which lots of "CHILD" class are inherited.

For the system is real-time embedded system,
"new" operation is applied here.

I want to pre-allocate a series of general
"CHILD" instances. Every time I want a specified
"CHILD" , I get one from a link list.

In C++, virtual class "FATHER" can not be
instanced.So the method I can bring to my mind
is to instance each "CHILD" class, and every
time I want a specified "CHILD", I get a memory
From a link list, and "memcpy" it.

This "memcpy" copy not only internal data ,but
virtual funtion table.

The method I memtion above may not be clever method.

I wander if there are better methods.

Thank you for your readimg this thread.
Jul 22 '05 #1
2 1856
On Mon, 15 Dec 2003 20:03:30 +0800, "pandy.song" <pa****@263.net>
wrote:
In my project, there is a class "Father",
from which lots of "CHILD" class are inherited.

For the system is real-time embedded system,
"new" operation is applied here.

I want to pre-allocate a series of general
"CHILD" instances. Every time I want a specified
"CHILD" , I get one from a link list.

In C++, virtual class "FATHER" can not be
instanced.So the method I can bring to my mind
is to instance each "CHILD" class, and every
time I want a specified "CHILD", I get a memory
From a link list, and "memcpy" it.

This "memcpy" copy not only internal data ,but
virtual funtion table.
You can't safely memcpy objects of non-POD type. Depending on the
implementation (and presense of e.g. virtual inheritence) it may not
work. This ignores the possible problems of double deletion, etc.

The method I memtion above may not be clever method.

I wander if there are better methods.


Use placement new. E.g. to get a new CHILD1 object, do this:

void* mem = obtain_memory_from_pool(sizeof(CHILD1));
CHILD1* child1 = new (mem) CHILD1(params);

//to delete:
child1->~CHILD1(); //(or as a Father*)
return_memory_to_pool(child1);
Or, alternatively, provide new/delete in Father. e.g.

class Father
{
public:
virtual ~Father();
//etc.

void* operator new(size_t amount)
{
//return "amount" of memory from linked list
}

void* operator delete(void* ptr, size_t amount)
{
//return memory to linked list.
}
};

Finally, check out boost's pool library:
http://www.boost.org/libs/pool/doc/index.html

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #2
Thank you very much.

I tried both methods, they work great well.
"tom_usenet" <to********@hotmail.com> wrote in message
news:7d********************************@4ax.com...
On Mon, 15 Dec 2003 20:03:30 +0800, "pandy.song" <pa****@263.net>
wrote:
In my project, there is a class "Father",
from which lots of "CHILD" class are inherited.

For the system is real-time embedded system,
"new" operation is applied here.

I want to pre-allocate a series of general
"CHILD" instances. Every time I want a specified
"CHILD" , I get one from a link list.

In C++, virtual class "FATHER" can not be
instanced.So the method I can bring to my mind
is to instance each "CHILD" class, and every
time I want a specified "CHILD", I get a memory
From a link list, and "memcpy" it.

This "memcpy" copy not only internal data ,but
virtual funtion table.


You can't safely memcpy objects of non-POD type. Depending on the
implementation (and presense of e.g. virtual inheritence) it may not
work. This ignores the possible problems of double deletion, etc.

The method I memtion above may not be clever method.

I wander if there are better methods.


Use placement new. E.g. to get a new CHILD1 object, do this:

void* mem = obtain_memory_from_pool(sizeof(CHILD1));
CHILD1* child1 = new (mem) CHILD1(params);

file://to delete:
child1->~CHILD1(); file://(or as a Father*)
return_memory_to_pool(child1);
Or, alternatively, provide new/delete in Father. e.g.

class Father
{
public:
virtual ~Father();
file://etc.

void* operator new(size_t amount)
{
file://return "amount" of memory from linked list
}

void* operator delete(void* ptr, size_t amount)
{
file://return memory to linked list.
}
};

Finally, check out boost's pool library:
http://www.boost.org/libs/pool/doc/index.html

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

Jul 22 '05 #3

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

Similar topics

2
3088
by: GriffithsJ | last post by:
Hi I have been given some text that needs to be displayed on a web page. The text is pre-formatted (includes things like lists etc) and displays okay if I wrap it using the <pre/> tag. ...
21
10176
by: Headless | last post by:
I've marked up song lyrics with the <pre> tag because it seems the most appropriate type of markup for the type of data. This results in inefficient use of horizontal space due to UA's default...
7
18513
by: Alan Illeman | last post by:
How do I set several different properties for PRE in a CSS stylesheet, rather than resorting to this: <BODY> <PRE STYLE="font-family:monospace; font-size:0.95em; width:40%; border:red 2px...
5
24083
by: Porthos | last post by:
I'm authoring an XML document and using the <pre> html tag for the portions that are not dynamically generated. The <pre> text is displaying in a smaller font size (and I believe different font)...
5
718
by: Michael Shell | last post by:
Greetings, Consider the XHTML document attached at the end of this post. When viewed under Firefox 1.0.5 on Linux, highlighting and pasting (into a text editor) the <pre> tag listing will...
1
4720
by: R0bert Nev1lle | last post by:
Here's my next IE challenge (or frustration). It deals with the overflow attribute. Overflow property was a challenge on my page since the page emulates position fixed for IE. The present...
7
2732
by: Rocky Moore | last post by:
I have a web site called HintsAndTips.com. On this site people post tips using a very simply webform with a multi line TextBox for inputing the tip text. This text is encode to HTML so that no...
9
5528
by: Eric Lindsay | last post by:
I can't figure how to best display little snippets of shell script using <pre>. I just got around to organising to bulk validate some of my web pages, and one of the problems occurs with Bash...
7
4840
by: Paul Connolly | last post by:
char *s = "Hello"; s = 'J'; puts(s); might print "Jello" in a pre-ANSI compiler - is the behaviour of this program undefined in any pre-ANSI compiler - or would it always have printed "Jello"...
0
7251
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7367
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7089
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7517
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5673
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1581
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
790
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
451
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.