destructor question

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2009
Posts: 14
Reputation: yakovm is on a distinguished road 
Solved Threads: 0
yakovm yakovm is offline Offline
Newbie Poster

destructor question

 
1
  #1
May 31st, 2009
  1. typedef
  2. vector<string> pipeline;
  3. typedef
  4. vector<pipeline> subPipelineStrings;
  5. subPipelineStrings* m_subPipelineStrings
  6. m_subPipelineStrings =
  7. new subPipelineStrings(3);
  8. string *s1 = new string("s1");
  9. string *s2 = new string("s2");
  10. string *s3 = new string("s3");
  11.  
  12.  
  13. for (int i = 0; i < m_subPipelineStrings ->size();++i)
  14. {
  15. pipeline &p = m_subPipelineStrings->at(i);
  16. p.push_back(*s1) ;
  17. p.push_back(*s2) ;
  18. p.push_back(*s3) ;
  19. }
The question : How do i destruct m_subPipelineStrings ?
Thank you in advance!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,672
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: destructor question

 
0
  #2
May 31st, 2009
Stay away from those pointers.

lines 8-10: delete those lines

lines 16-18:
  1. p.push_back(string("s1")) ;
  2. p.push_back(string("s2")) ;
  3. p.push_back(string("s3")) ;

With the above changes you don't have to worry about a destructor because the vector and string classes with destroy themselves.
Last edited by Ancient Dragon; May 31st, 2009 at 2:34 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 14
Reputation: yakovm is on a distinguished road 
Solved Threads: 0
yakovm yakovm is offline Offline
Newbie Poster

Re: destructor question

 
0
  #3
May 31st, 2009
It my need really...
Actually the strings s1,s2 are generated in some other function in dynamic way.
Please help me if you can
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,672
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: destructor question

 
0
  #4
May 31st, 2009
The code you posted appears to contain a memory leak. You don't have to worry about destroying those vectors because the vector class will destroy itself when the class or program terminates.

If s1, s2 and s3 are being allocated somewhere else with new operator, then you need to delete them after push_back() is called (after line 19 of the code snippet you posted).
Last edited by Ancient Dragon; May 31st, 2009 at 2:49 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 14
Reputation: yakovm is on a distinguished road 
Solved Threads: 0
yakovm yakovm is offline Offline
Newbie Poster

Re: destructor question

 
0
  #5
May 31st, 2009
Originally Posted by Ancient Dragon View Post
The code you posted appears to contain a memory leak. You don't have to worry about destroying those vectors because the vector class will destroy itself when the class or program terminates.

If s1, s2 and s3 are being allocated somewhere else with new operator, then you need to delete them after push_back() is called (after line 19 of the code snippet you posted).
Thank you very much it worked!!!
Last edited by yakovm; May 31st, 2009 at 2:58 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 265 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC