Allocating memory with STL
Hi,
I need to allocate a chunk of memory which it can be explicilty deallocated via by a function call and implicilty deallocated..( when it goes out of scope)
The auto_ptr doesn''t seem to do the job since it requires a class dtor
I''ve been looking at vectors but it can''t seems to free the memory explicilty..
Any ideas??
Well, if you need to have this memory allocated for only part of a function, you can force a new scope:
If you need more functionality than this, it looks like you''ll need to make your own class.
myfunc (){ // do some stuff // now we need some memory { // <-- don''t need any conditional, just make new scope vector<char> v (100); // allocate 100 bytes // do some stuff } // <-- deletes the 100 bytes when you get here // do your other stuff}
If you need more functionality than this, it looks like you''ll need to make your own class.
You could simply delete the thing that auto_ptr points to, e.g.
If you call the release function on auto_ptr, it will give up ownership of the pointer (i.e. it wont delete it when it goes out of scope) and you can safely delete the returned pointer.
I noticed that you say "chunk of memory", which give me the impression your allocating an array. If you are, then std::auto_ptr isn''t what you should be using. It uses ''delete'' and not ''delete []'', so you could end up with only the first element being deleted leaving you with a leak.
You should probably use something like std::vector, you can call its resize function to resize to 0 which call the dtor for each item in the list. But it wont free the memory (vector allocates memory in pools so it doesn''t need to new/delete as often). To force a vector to free it''s memory (which will also delete the items in it. You need to swap it with another vector (this will make the two vectors swap their internal array. e.g.
You can make the code shorter by swapping with an unnamed temporary vector, e.g.
If you do it this way the unnamed vector will be destroyed on the same line. The method you use is up to you.
Alternatively, you use a auto pointer that works with arrays, have a look at www.boost.org for some pretty good ones.
std::auto_ptr pSomthing = new something;//some code doing thingsif(need_to_delete) delete pSomthing.release();
If you call the release function on auto_ptr, it will give up ownership of the pointer (i.e. it wont delete it when it goes out of scope) and you can safely delete the returned pointer.
I noticed that you say "chunk of memory", which give me the impression your allocating an array. If you are, then std::auto_ptr isn''t what you should be using. It uses ''delete'' and not ''delete []'', so you could end up with only the first element being deleted leaving you with a leak.
You should probably use something like std::vector, you can call its resize function to resize to 0 which call the dtor for each item in the list. But it wont free the memory (vector allocates memory in pools so it doesn''t need to new/delete as often). To force a vector to free it''s memory (which will also delete the items in it. You need to swap it with another vector (this will make the two vectors swap their internal array. e.g.
vector vec;//use vec{ vector temp; temp.swap(vec); //temp holds evthing that was in vec, and vec is empty} //temp goes out of scope and frees its memory
You can make the code shorter by swapping with an unnamed temporary vector, e.g.
vector().swap(vec);
If you do it this way the unnamed vector will be destroyed on the same line. The method you use is up to you.
Alternatively, you use a auto pointer that works with arrays, have a look at www.boost.org for some pretty good ones.
Good one, Wilka. swap rules. It''s rarely appropriate, but when it does it works wonders.
Thanks..
I suddenly thought another solution that makes me feel so stupid.. I can call the vector class dtor explicitly to free the memory..
Sigh.. stupid mental block..
..
I suddenly thought another solution that makes me feel so stupid.. I can call the vector class dtor explicitly to free the memory..
Sigh.. stupid mental block..
..
quote:
I suddenly thought another solution that makes me feel so stupid.. I can call the vector class dtor explicitly to free the memory..
That''s not a good idea. If you call the dtor of the vector, it''ll still be called again by the compiler when the vector goes out of scope. If your lucky, it''ll crash straight away due to heap corruption. If not, you could end up with a hard to find bug that doesn''t appear until a long time after the problem was caused. It''s also possible that it works fine because the version of vector your using marks itself as being empty in its dtor.
You should either use the swap() method, or put the vector in an auto pointer and delete it directly from there (delete ptr.release()).
Hmm, good point. The dtor may not marked the vector as being empty, esp in release versions.
Hmm, could you show me how to do that? I can''t seem to get that to work.
quote: Original post by Wilka
put the vector in an auto pointer and delete it directly from there (delete ptr.release()).
Hmm, could you show me how to do that? I can''t seem to get that to work.
quote:
Hmm, could you show me how to do that? I can''t seem to get that to work.
Somthing like this should do what you want:
//create an auto pointer to a vector of 100 intsauto_ptr<vector<int> > pVec(new vector<int>(100)); //remember to deference the auto ptr before you use it(*pVec)[1] = 10;//use -> to access functionspVec->push_back(15);pVec->at(9) = 894;//etc...//delete the vector when your donedelete pVec.release();
You need the space between the two less-thans in the auto_ptr, if you don''t put a space the compiler will think it''s a right shift.
quote:
Hmm, good point. The dtor may not marked the vector as being empty, esp in release versions.
That''s not the only problem with calling a dtor explicitly when your not supposed to. The destructor is more than whatever you type inside the function body, it also calls the dtor for each of the members (and the basses which destruct their members). Even if you only store basic types (e.g. int, void*) it could still cause problems. Try this program on your compiler:
class Inner{public: ~Inner() { cout << "Inner::~Inner" << endl; }};class Outer{ Inner m_inner;};int main(int argc, char *argv[]){ Outer test; test.~Outer(); cout << "after dtor" << endl; return 0;}
I tested it on Borlands and Microsofts compiler, and they both give the smame output:
Inner::~Inner
after dtor
Inner::~Inner
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement