C++ how to avoid calling destructor

Started by
31 comments, last by iMalc 11 years, 3 months ago

You could emplace_back(), if your compiler(s) support it (C++11 once again).

Advertisement

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);
}

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);
}


That won't suppress the some_class destructor being called when array is deallocated or otherwise shuffles elements.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

If your element is default constructed, you can just resize the vector to its size plus one. vector::resize() creates default constructed objects, you can them modify the member through accessing it from the vector if necessary.

In most cases, this will not cause a re-allocation of memory backing the vector, and in cases where it does it was unavoidable anyhow. However, if you will be constructing several of these objects at once, you can hit them all at once by resizing to the size of the array plus the required number of objects.

Also, be wary of any pointers you're holding, they become invalid if the memory backing the vector is re-allocated -- but again, this would be true of the standard solution. Just something to be aware of.

[Edit] -- don't you hate it when you read responses, and in your head that changes the nature of the question? Anyhow, if your ends are to eliminate the redundant initialization, copy of, and deconstruction of temp just to put it in vector, then what I suggested is useful. If your ends are different, well, you should probably explain or find different ends, because they're opaque to me.

throw table_exception("(? ???)? ? ???");

That won't suppress the some_class destructor being called when array is deallocated or otherwise shuffles elements.

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.
Ah, I see.

It's still a bad idea of some_class does any allocations or controls any resources, obviously, as they will leak. I suspect if some_class has a nontrivial destructor this is likely to be the case.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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...
}


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.

I'd just recommend (properly) using smart pointers (that is, just a plain old std::shared_ptr<some_class> temp(new some_class())).

Edit: I actually like Ravyne's idea. You could do:

array.resize(array.size() + 1);
some_class& back = array.back(); // You only need to do this if you want to edit the last element
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
It should compile, I would be interested in knowing why it doesnt for you.
On g++ I need tr1/memory and tr1/functional headers included.

As for the OP, he is the one that didn't want to call the constructor. I didn't question it lol (ironically in fear of losing rep haha). The "some_class" might add itself to a cache pool on creation and so not want to be deleted once the std::vector goes out of scope (i.e how Irrlicht handles meshes etc...)

As for (properly) using smart pointers, once you get this to compile, it works really nicely for C libraries (with the appropriate deleter func added) without needing to wrap loads of stuff in C++ classes. Though the problem in this thread isnt perhaps the best use-case for it.
http://tinyurl.com/shewonyay - Thanks so much for those who voted on my GF's Competition Cosplay Entry for Cosplayzine. She won! I owe you all beers :)

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.

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. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Yeah you read it whilst I was still fixing my post.<br /><br />Internet is too slow and these forums are a tad broken but I finally got my post submitted.<br /><br />I am off for a beer now lol.
http://tinyurl.com/shewonyay - Thanks so much for those who voted on my GF's Competition Cosplay Entry for Cosplayzine. She won! I owe you all beers :)

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.

This topic is closed to new replies.

Advertisement