Nested STL containers

Oct 16, 2009 at 11:21am
Hello!
I have the question which could be considered as OS-dependent or compiler-dependent maybe, but I'm not sure.

So, I'm using nested containers, defining new types, for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
extern struct wdate_t; //some structure including several int's
typedef vector<wdate_t> v_wdate_t;
typedef map<string,v_wdate_t> m_str_vwdate_t;
typedef map<string,m_str_vwdate_t> m_str_mstrvwdate_t;
typedef map<unsigned long,m_str_mstrvwdate_t> m_ul_mstrmstrvwdate_t;
typedef map<string,m_ul_mstrmstrvwdate_t> svc_t;

void main(){
wdate_t wdate;
svc_t svc;
...
while(true){
   for(int i=0;i<100000;i++){
      //getting object wdate of wdate_t type
      get(i,&wdate);//just filling its fields
      //inserting into container
      svc[<string1>][<ulong1>][<string2>][<string3>].push_back(wdate);
      //<stringi>- just variable of string type, <ulong1> - some unsigned long variable
   }
   ...
   svc.clear();//erasing all elements from container
}
...
}


I compile this under Solaris using CC-compiler, version 5.8. Program works fine, but I have memory leakage... I have done several tests and the problem appears like program doesn't free memory when I invoke svc.clear(). But after invokation of clear() method, svc contains zero elements.
With each iteration program allocates new memory block (I can monitor this with top utility).
My question is: what's wrong?
Last edited on Oct 17, 2009 at 10:33am
Oct 16, 2009 at 11:53am
One possibility is vector's implementation.

vector<> never releases the memory it allocates, even when you clear() it. (It only releases the memory
on destruction). One of my pet peeves re: vector.

Oct 17, 2009 at 8:33am
jsmith, thank you for your answer.
It's strange, but under VC2005 vector successfully releases the memory by calling clear() method. Did you mean exactly CC compiler in your post?
Last edited on Oct 17, 2009 at 8:36am
Oct 17, 2009 at 8:38am
The exact behavior of clear() is probably implementation-dependent. All it guarantees is that the size will be zero after the call, and that all destructors will be called.
If you need to guarantee that the memory will be freed, use a dynamically-allocated vector, then delete and reallocate when necessary.
Oct 17, 2009 at 10:19am
But I use vector nested in map. Doesn't it mean that vector is allocated dynamically and map::clear() just implements delete for each of its vector-objects?
Last edited on Oct 17, 2009 at 10:21am
Topic archived. No new replies allowed.