Question:
How to clear the contents of a vector (C++)?
Don't sue me!
2011-07-23 14:35:17 UTC
I'm using a vector class I got from the open source game Cube 2: Sauerbraten (tools.h, link below).
My vector objects are leaking memory (total 400+ kb each time), mostly because I still have no idea how to clear its contents.

I tried .shrink(0), .remove(0, .length()) but the leak was still there.
.deletecontents() deletes the targets of the pointers, which is of no use since I have objects inside the array.

The objects are of class "enemy" which I'm using to represent enemies in my game.
enemy doesn't have a destructor since neither it nor the base objects it inherits from have any pointers.

Can you give me any tips or suggestions on how to correctly empty my vectors and fix this?



Links:
tools.h: http://code.google.com/p/remod-sauerbraten/source/browse/trunk/shared/tools.h?r=91
Three answers:
husoski
2011-07-23 16:29:21 UTC
Use its destructor, I guess. That's the only place delete is called, outside of grow() the deletecontents/deletearrays stupid methods.



Or, since it's not really object oriented (struct types for objects? really??) then simply:



myvec.shrink(0); // do this first!!!

delete[] myvec.buf; // This is the delete you're looking for.

myvec.buf = NULL; // or 0, if you dislike self-documenting code



That bit of code has more holes than just your memory leak, though. Sometimes it calls destructors on items it deletes (like with shrink()) and sometimes it doesn't (like with pop() or remove()). It's a good thing you don't have pointers in your objects.



What do you get out of this that the standard C++ vector<> class won't do? Other than the memory leak, I mean, or your namespace polluted with its pointless macros? You can fix it by adding the hack above as a method. But then you're still stuck with a 1990's era pre-standard C++ "library".
cancela
2016-11-29 08:38:17 UTC
An lvalue reference is surely in basic terms syntactic sugar for a pointer. An lvalue reference can in basic terms exist for an merchandise it somewhat is explicitly instantiated. An rvalue reference is a particular pointer to an merchandise that the compiler would have created quickly for itself. you may have an rvalue reference for the effects of a calculation, even a relentless. The rvalue reference must be carried out in basic terms via ignoring the && and storing the value itself. besides the shown fact that making use of && facilitates the compiler to greater effective music the reason of the code, which comes into play whilst it optimises the code. it is somewhat important whilst one template makes use of yet another. So, to respond to your question approximately no be counted if b = a or b = std:circulate(a) is greater helpful, no, they'll collect to an identical code.
Blackcompe
2011-07-23 14:44:42 UTC
Call vector::clear: http://www.cplusplus.com/reference/stl/vector/clear/ . Also make sure you've defined destructors for the bounded type.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...