• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Aroidzap

C++ how to avoid calling destructor

32 posts in this topic

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

0

Share this post


Link to post
Share on other sites
Not really. Why don't you want the destructor called?

If you are trying to save the copy, you can look into C++11's move semantics.
2

Share this post


Link to post
Share on other sites

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?

1

Share this post


Link to post
Share on other sites
ok... so... is it possible to push_back pointer to "temp" (create temp using some_class *temp = new some_class;) in array? Edited by Aroidzap
0

Share this post


Link to post
Share on other sites
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...
}

-4

Share this post


Link to post
Share on other sites

I think I will try to solve my problem another way... I don't want to make it messy. But thanks for help ;)

0

Share this post


Link to post
Share on other sites
Whoever decreased my rep, I am hoping you did so for the nullFunc stuff rather than the std::tr1::shared_ptr because although that looks messy, it is pretty standard C++.
0

Share this post


Link to post
Share on other sites
There are many things you could do, like having a container of pointers, a container of smart pointers or a Boost pointer container. But unless we know what you are trying to do and why it is a problem that the destructor is being called, we can't really give you much advice.
1

Share this post


Link to post
Share on other sites

Whoever decreased my rep, I am hoping you did so for the nullFunc stuff rather than the std::tr1::shared_ptr because although that looks messy, it is pretty standard C++.

It's probably because of the misspelled "its". smile.png
0

Share this post


Link to post
Share on other sites

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

2

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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. Edited by Ravyne
1

Share this post


Link to post
Share on other sites

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. Edited by SiCrane
2

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

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
Edited by Cornstalks
0

Share this post


Link to post
Share on other sites
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. Edited by Karsten_
-1

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites
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.
-2

Share this post


Link to post
Share on other sites

Why don't you want the destructor called?

I quoted this post, to re-affirm that it is the "right" question.

 

If you are trying to save the copy, you can look into C++11's move semantics.

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

 

I quoted the above two posts to re-affirm that they are the "right" answer. Both require C++11.

If you aren't C++11, you could do something like this instead:

std::vector<some_class> array;

void pushback_function()
{
    array.push_back(some_class());
    array.back().heavyInitializationStuff();
}

But that violates RAII, as so should be a questionable move to make. If you don't have C++11, your heavy class could implement some kind of "swap" function just for itself, to transfer ownership of data pointers or something.

Instead, I'll ask my own "right" question: Is this an actual need for being optimized after measuring your existing application's performance using profiling to find the bottlenecks, or is this pre-optimization? I wouldn't be surprised if your compiler optimized your existing code as it is.

Why is the copy such a huge deal?

Well, your actual question was, how do you avoid the destructor. So I guess my question should be, why is the destructor such a big deal? What are you doing in it?

1

Share this post


Link to post
Share on other sites

Personally, I always use pointers in container templates. This avoid unnecessary memory copies and constructor/destructor calls.

I'm a bit of a memory nazi though, so I prefer doing the new and delete myself.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0