Re: Memory leak on small stl vectors
In article <1151711252.217711.143700@b68g2000cwa.googlegroups.com>,
dr_num <hj000vi@vug.uni-duisburg.de> writes
vector<size_t> vp;
for(size_t iii = 0; iii<3; iii++) {
vp.push_back(iii);
};
vector<size_t>* tvp;
tvp = new vector<size_t>[1000000];
// copy
for(size_t i = 0; i<1000000; i++) {
tvp[i] = vp;
};
// clear
for(size_t i = 0; i<1000000; i++) {
tvp[i].clear();
};
// delete
delete [] tvp;
Rather than try to get my mind round such complexity (I hate high level
use of the new expression as it almost always leads to problems. If
nothing else you have a problem with exception safety. Consider
typedef std::vector<unsigned int> vs_t;
// note that I am deeply suspicious of your use
// of size_t in this context but note that my
// change can be changed back by a single
// change to my code
vs_t vp(3);
for(int i(0); i != 3; ++i){
vp[i] = i;
}
std::vector<vs_t> tvp(1000000, vp);
// tvp will be cleaned up when it goes out of scope
// including when an exception is thrown through
// the code block between the point of definition and
// the end of the block.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' and "You Can Program in C++"
see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]