473,394 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

C++ memory loss (particularly lists of structs)

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
3 1672
weaknessforcats
9,208 Expert Mod 8TB
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
1,251 Expert 1GB
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
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

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

Similar topics

3
by: Sourin | last post by:
Hi all, I am trying to write code for my experiments. My work involves huge datasets, and as such my code needs to be memory efficient. I did some hand calculations regarding the amount of...
4
by: David Thorp | last post by:
New to this list (first post), and relatively new to C, so hi everyone... If anyone can help me with this I'll be most grateful... The following code is from a rather elaborate (for me) program...
3
by: Christian F | last post by:
Hi, I'm a C-newbie and I would like to know if I am doing something wrong in the code below. It is working, but I'm afraid it might not be correct because I don't really understand everything of...
15
by: fix | last post by:
Hi all, I am writing a program using some structs, it is not running and I believe it is because there's some memory leak - the debugger tells me that the code causes the problem is in the malloc...
19
by: Jon Davis | last post by:
I'm reposting this because I really need some advice. I have a web app that makes many queries to the database on every page. In order to save development effort, I've consolidated all database...
0
by: xiaoling he | last post by:
I try to detect potential memory management bugs of my program with valgrind. (PostgreSQL is at version 8.0 beta2. Operating System is Red Hat Enterprise Linux 3. Valgrind is at version 2.2.0.) ...
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
12
by: Frodo Baggins | last post by:
Hi I need to know the size of the memory block pointed to by a char* in a function receiving this pointer. Typically, this pointer points to a string. strlen() will not do the job since sometimes...
10
by: Chris Saunders | last post by:
Here is the declaration of a struct from WinIoCtl.h: // // Structures for FSCTL_TXFS_READ_BACKUP_INFORMATION // typedef struct _TXFS_READ_BACKUP_INFORMATION_OUT { union { //
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.