943,542 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 437
  • C++ RSS
May 31st, 2009
1

destructor question

Expand Post »
c++ Syntax (Toggle Plain Text)
  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!
Similar Threads
Reputation Points: 46
Solved Threads: 0
Light Poster
yakovm is offline Offline
27 posts
since May 2009
May 31st, 2009
0

Re: destructor question

Stay away from those pointers.

lines 8-10: delete those lines

lines 16-18:
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,947 posts
since Aug 2005
May 31st, 2009
0

Re: destructor question

It my need really...
Actually the strings s1,s2 are generated in some other function in dynamic way.
Please help me if you can
Reputation Points: 46
Solved Threads: 0
Light Poster
yakovm is offline Offline
27 posts
since May 2009
May 31st, 2009
0

Re: destructor question

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,947 posts
since Aug 2005
May 31st, 2009
0

Re: destructor question

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.
Reputation Points: 46
Solved Threads: 0
Light Poster
yakovm is offline Offline
27 posts
since May 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: basis server creation fail using pipes ???
Next Thread in C++ Forum Timeline: sudoku c++ help :D





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC