Consider the following code:
class A
{
B* b; // an A object owns a B object
A() : b(NULL) { } // we don't know what b will be when constructing A
void calledVeryOften(…)
{
if (b)
delete b;
b = new B(param1, param2, param3, param4);
}
};
My goal: I need to maximize performance, which, in this case, means minimizing the amount of memory allocations.
The obvious thing to do here is to change B* b; to B b;. I see two problems with this approach:
- I need to initialize
bin the constructor. Since I don’t know whatbwill be, this means I need to pass dummy values to B’s constructor. Which, IMO, is ugly. - In
calledVeryOften(), I’ll have to do something like this:b = B(…), which is wrong for two reasons:- The destructor of
bwon’t be called. - A temporary instance of B will be constructed, then copied into
b, then the destructor of the temporary instance will be called. The copy and the destructor call could be avoided. Worse, calling the destructor could very well result in undesired behavior.
- The destructor of
So what solutions do I have to avoid using new? Please keep in mind that:
- I only have control over A. I don’t have control over B, and I don’t have control over the users of A.
- I want to keep the code as clean and readable as possible.
I liked Klaim’s answer, so I wrote this up real fast. I don’t claim perfect correctness but it looks pretty good to me. (i.e., the only testing it has is the sample
mainbelow)It’s a generic lazy-initializer. The space for the object is allocated once, and the object starts at null. You can then
create, over-writing previous objects, with no new memory allocations.It implements all the necessary constructors, destructor, copy/assignment, swap, yadda-yadda. Here you go:
In your case, just create a member in your class:
lazy_object<B>and you’re done. No manual releases or making copy-constructors, destructors, etc. Everything is taken care of in your nice, small re-usable class. 🙂EDIT
Removed the need for vector, should save a bit of space and what-not.
EDIT2
This uses
aligned_storageandalignment_ofto use the stack instead of heap. I used boost, but this functionality exists in both TR1 and C++0x. We lose the ability to copy, and therefore swap.And there we go.