Experts, how to avoid calling local object's destructor?
Posted: Wed Oct 12, 2011 7:40 am
Hi C++ experts,
I've just learned how to call constructor/destructor without calling allocator/deallocator. My objective is to establish a custom container which contain objects like std::vector but is more efficient. The push_back() does not need to recreate the object, but rather allocate space and copy in the object only. But now the problem comes!
So the problem is that in func2(), the local object A somehow decides to be added into MyVector, it's contents gets copied into MyVector, internal buffer pointers need to be kept, but when func2() returns, ~A() will be called, causing the A in MyVector to have internal buffer pointers invalid.
Under my specific situation, I have some constraints:
1. I am not going to use vector of pointer because MyClass is going to be created and destroyed frequently, dynamic memory allocation hits performance. In this case, I preallocate space for MyClass, no dynamic memory allocation. (Internal buffers of MyClass is not destroyed everytime, sometimes just gets transferred.)
2. I am not going to increment the std::vector first and then get a pointer to newly created MyClass object in the std::vector to access it, because sometimes the local object A may not need to be pushed into MyVector.
3. I am not going to increment the std::vector first, get the pointer to access new element, then delete it if no need to be inserted because MyVector is in fact a circular buffer accessed by multiple threads, creating temporary object causes problems.
There're some other constraints, and in conclusion, I am forced to do it in exactly this way, any way to achieve that?
Wang Xuancong
I've just learned how to call constructor/destructor without calling allocator/deallocator. My objective is to establish a custom container which contain objects like std::vector but is more efficient. The push_back() does not need to recreate the object, but rather allocate space and copy in the object only. But now the problem comes!
Code: Select all
MyClass{
int x,y;
char *buf1,*buf2;
MyClass(int size){
buf1 = new int [size];
//... do some initialization, very slow
}
~MyClass(){
//...do some clean up before releasing buffers, very slow
delete [] buf1;
}
operator = (MyClass &rhs){
// very complicated and time consuming
}
};
MyVector{
int size, max_size;
MyClass *m_data;
MyVector(int _size){
m_data=new char [sizeof(MyClass)*_size];
size=0;
max_size=_size;
}
void push_back(MyClass &in){
//... check for size overflow and relocate space if neccessary
memcpy(&m_data[size],&in,sizeof(MyClass));
size++;
}
}
void func1(){
MyClass A(100);
std::vector <MyClass> v;
v.push_back(A); // object gets recreated by std::vector and assigned, which is slow and unneccessary
} // local object A is deallocated and destroyed, which is slow and unneccessary
void func2(){
MyClass A(100);
MyVector <MyClass> v;
v.push_back(A); // object gets copied in directly, neither constructor/allocator nor assigner gets called, which is fast
} // now the problem comes, how to deallocate local object A without calling its destructor
Under my specific situation, I have some constraints:
1. I am not going to use vector of pointer because MyClass is going to be created and destroyed frequently, dynamic memory allocation hits performance. In this case, I preallocate space for MyClass, no dynamic memory allocation. (Internal buffers of MyClass is not destroyed everytime, sometimes just gets transferred.)
2. I am not going to increment the std::vector first and then get a pointer to newly created MyClass object in the std::vector to access it, because sometimes the local object A may not need to be pushed into MyVector.
3. I am not going to increment the std::vector first, get the pointer to access new element, then delete it if no need to be inserted because MyVector is in fact a circular buffer accessed by multiple threads, creating temporary object causes problems.
There're some other constraints, and in conclusion, I am forced to do it in exactly this way, any way to achieve that?
Wang Xuancong