Hi, I've got code like this :
std::vector<some_class> array;
void pushback_function()
{
some_class temp;
array.push_back(temp);
}
Is it somehow possible to avoid calling destructor of temp
Posted 29 January 2013 - 12:47 PM
Every object that is created needs to be destroyed. There is a very tiny exception to the rule and that is if you are basically writing your own memory manager. But I doubt that is the case here.
What are you trying to accomplish? Why don't you want your destructor to get called?
Posted 29 January 2013 - 12:53 PM
array.push(std::tr1::shared_ptr<some_class>(new temp(), std::tr1::bind(std::tr1::placeholders::_1, nullFunc));
...
void nullFunc(some_class* unused)
{
// do nothing...
}
Posted 29 January 2013 - 01:07 PM
Posted 29 January 2013 - 01:10 PM
ok... so... is it possible to push_back pointer to "temp" (create temp using some_class *temp = new some_class;) in array?
Sure, you can create a std::vector<some_class *>, and then push pointers into it instead. Just remember to delete each one when you are actually done with it.
You can also use std::shared_ptr, or (from C++11 onwards) std::unique_ptr, to avoid having to remember to delete the objects in the end.
But I'm curious: how much work is this destructor doing, that you are so adamant to avoid calling it? In general, a copyable class should do very little work in its destructor (and if your class isn't meant to be copyable, well, then you need to take the pointer approach to start with).
Tristam MacDonald - SDE @ Amazon - swiftcoding [Need to sync your files via the cloud? | Need affordable web hosting?]
Posted 29 January 2013 - 01:44 PM
You could do this, but I don't recommend it:
void pushback_function()
{
char storage[sizeof(some_class)];
some_class *temp = new(storage) some_class;
array.push_back(*temp);
}
Posted 29 January 2013 - 01:51 PM
That won't suppress the some_class destructor being called when array is deallocated or otherwise shuffles elements.You could do this, but I don't recommend it:
void pushback_function() { char storage[sizeof(some_class)]; some_class *temp = new(storage) some_class; array.push_back(*temp); }
Posted 29 January 2013 - 02:11 PM
Edited by Ravyne, 29 January 2013 - 02:24 PM.
Posted 29 January 2013 - 02:13 PM
I'm not sure that's an issue in this case, as the OP only specified suppressing the destructor of the temp object. One problem with the locally allocated char array is that it may not have the correct alignment for some_class. A similar example from the C++98 standard had to be changed in the C++03 standard to use dynamic allocation of the char array so that alignment requirements were met.That won't suppress the some_class destructor being called when array is deallocated or otherwise shuffles elements.
Edited by SiCrane, 29 January 2013 - 02:14 PM.
Posted 29 January 2013 - 02:19 PM
Posted 29 January 2013 - 02:20 PM
That's just... horrible (plus it doesn't even compile). Why would you even recommend that? I mean, what's the point of even using shared_ptr here? It accomplishes nothing. Might as well use a raw pointer and never call delete on it... You're leaking memory here.Perhaps push the memory contained within an std::tr1::shared_ptr onto the vector but set it's deleter function to a function that does nothing.
array.push(std::tr1::shared_ptr<some_class>(new temp(), std::tr1::bind(std::tr1::placeholders::_1, nullFunc)); ... void nullFunc(some_class* unused) { // do nothing... }
array.resize(array.size() + 1); some_class& back = array.back(); // You only need to do this if you want to edit the last element
Edited by Cornstalks, 29 January 2013 - 02:23 PM.
Posted 29 January 2013 - 02:51 PM
Edited by Karsten_, 29 January 2013 - 03:04 PM.
Posted 29 January 2013 - 02:56 PM
As for (properly) using smart pointers, once you get this to compile, it works really nicely for C libraries without needing to wrap loads of stuff in C++ classes.
What about it "works nicely"? From where I'm standing, you just tried to spin guaranteed memory leaks as a "feature"...
The OP said he wanted to avoid calling the destructor on the temp object, not altogether.
Tristam MacDonald - SDE @ Amazon - swiftcoding [Need to sync your files via the cloud? | Need affordable web hosting?]