423,850 Members | 1,661 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,850 IT Pros & Developers. It's quick & easy.

C++ memory loss (particularly lists of structs)

P: 2
Hello.

I have been developing a piece of software in which I am trying to use lists of structs, and structs containing these lists. What I am finding is that I am losing memory somewhere along the lines. I will give somewhat of an example (I do not wish to post the exact code due to IP issues, but this is exactly what the problem section of the code does):

Expand|Select|Wrap|Line Numbers
  1. struct s1{
  2.     string a1;
  3.     string a2;
  4.     void print() { stuff here}
  5. };
  6.  
  7. struct s2{
  8.     string a1;
  9.     string a2;
  10.     list<s1> b1;
  11.     list<s1> b2;
  12.     list<s1> b3;
  13.     void print() { 
  14.         list<s1>::iterator Lis = b1.begin();
  15.         while(Lis!=b1.end()){
  16.             s1 an = *Lis;
  17.             Lis++;
  18.             an.print();     //and similarly for b2 and b3
  19.         }
  20.     }
  21. };
  22. ....
  23. ....
  24. s2 getdata(){
  25. s2 s;
  26. if(condition){
  27.     s.a1="a1";
  28.     s.a2="a2";
  29.     list<s1> x = get1();
  30.         list<s1>::iterator Lis = x.begin();
  31.         while(Lis!=x.end()){
  32.             s1 an = *Lis;
  33.             Lis++;
  34.             an.print();     //these print correctly
  35.         }
  36.     s.b1=x;
  37.     list<s1> y = get2();
  38.         Lis = y.begin();
  39.         while(Lis!=y.end()){
  40.             s1 an = *Lis;
  41.             Lis++;
  42.             an.print();
  43.         }
  44.     s.b2=y;
  45.     list<s1> z = get3();
  46.         Lis = z.begin();
  47.         while(Lis!=z.end()){
  48.             s1 an = *Lis;
  49.             Lis++;
  50.             an.print();
  51.         }
  52.     s.b3=z;
  53.  
  54. }
  55. s.print();              //this gives a different (and incorrect) result
  56. return s;
  57. }
  58.  
I get two different results from the two different sets of printing (in particular, during the s.print we get essentially x z z instead of x y z printed out, so to me it appears that the memory is getting lost). If this was vanilla C I would probably work it out with memcpy(), but I cannot convince memcpy() to work on lists etc.

It may be of importance that get1(), get2() and get3() all call the same function with slightly different parameters.

Any advice anyone can give would be greatly appreciated.
May 17 '07 #1
Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,196
Based on a cursory look I suspect:
Expand|Select|Wrap|Line Numbers
  1. //and similarly for b2 and b3
  2.  
in S2::print() isn't true. Are you sure it's doing x y z and not a typo where it's doing x z z???

I can't see the code so I'm just guessing.
May 17 '07 #2

AdrianH
Expert 100+
P: 1,251
I would hazard that lines like this:
Expand|Select|Wrap|Line Numbers
  1.             s1 an = *Lis;
  2.             // ...
  3.             list<s1> x = get1();
  4.  
are what are causing grief.

Try using references:
Expand|Select|Wrap|Line Numbers
  1.             s1& an = *Lis;
  2.             // ...
  3.             list<s1>& x = get1();
  4.  
and passing references unless this is not resonable for your application.


Adrian
May 17 '07 #3

P: 2
thanks, I will try that.

I would hazard that lines like this:
Expand|Select|Wrap|Line Numbers
  1.             s1 an = *Lis;
  2.             // ...
  3.             list<s1> x = get1();
  4.  
are what are causing grief.

Try using references:
Expand|Select|Wrap|Line Numbers
  1.             s1& an = *Lis;
  2.             // ...
  3.             list<s1>& x = get1();
  4.  
and passing references unless this is not resonable for your application.


Adrian
May 18 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.