deleting dynamic memory

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2007
Posts: 27
Reputation: rugae is an unknown quantity at this point 
Solved Threads: 0
rugae rugae is offline Offline
Light Poster

deleting dynamic memory

 
0
  #1
Nov 4th, 2007
I've know that a pointer to some dynamic memory must be deleted to prevent memory leaks.

Now if say a struct/array or some other data structure, each with its own dynamic memory parts. If that structure (class, file...etc) contains the data structure goes out of scope does that automatically free the memory pointed to by the pointers or does one still need to delete each one manually?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,636
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 469
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: deleting dynamic memory

 
0
  #2
Nov 4th, 2007
You only/always free the memory which you explicitly allocated. If you dynamically allocate memory for a struct variable, you need to explicitly free it even though the members it contains follow the automatic memory allocation scheme.

If your struct contains instances of other structs which in turn are dynamically allocated, you must make sure that the cleanup routine walks the entire object graph and explicitly frees the allocated memory, starting from the lowest rung all the way up the object hierarchy.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: deleting dynamic memory

 
0
  #3
Nov 4th, 2007
let us get this straight. it is usually a technical error for a class/struct to contain members which represent unencapsulated resources (unless the class/struct itself is an encapsulating wrapper for a *single* raw resource). here is a simple example:
  1. struct book
  2. {
  3. book( const char* t, const char* a ) ;
  4. ~book() ; // required
  5. book( const book& that ) ; // required
  6. book& operator= ( const book& that ) ; // required
  7. char* title ;
  8. char* author ;
  9. };
  10.  
  11. book::book( const char* t, const char* a )
  12. {
  13. size_t title_len = strlen(t) ;
  14. title = new char[ title_len+1 ] ;
  15. strcpy( title, t ) ;
  16.  
  17. size_t auth_len = strlen(a) ;
  18. author = new char[ auth_len+1 ] ; // this may fail (throw std::bad_alloc)
  19. // if it does, memory for title will leak. note: destructor of the book
  20. // being constructed will not (and should not) be called as the constructor
  21. // has not completed. so we have to scaffold this in a try/catch block.
  22. strcpy( author, a ) ;
  23. }
the case with the copy constructor would be identical; the assignment operator is going to be even more messy. and as an exercise, try writing this code correctly:
  1. struct foo { /* ... */ } ;
  2. struct ebook : public book
  3. {
  4. /*
  5.   ...
  6.   */
  7. foo* ptr ; // ebook 'owns' object pointed to by ptr
  8. ebook& operator= ( const ebook& that ) ; // implement this correctly
  9. };
and once you have tried your hand at it, have a look at http://www.concentric.net/~rtgillam/pubs/assign.html and http://www.icu-project.org/docs/pape...revisited.html

now compare this with
  1. struct book
  2. {
  3. book( const char* t, const char* a ) ;
  4. // compiler generated destructor, copy constructor, assignment
  5. std::string title ;
  6. std::string author ;
  7. };
  8.  
  9. book::book( const char* t, const char* a ) : title(t), author(a)
  10. {
  11. // constructor of author may fail (throw std::bad_alloc)
  12. // if it does, title, which has already been constructed will be
  13. // destroyed during the stack unwind; we have nothing to worry about.
  14. }
  1. struct foo { /* ... */ } ;
  2. struct ebook : public book
  3. {
  4. /*
  5.   ...
  6.   */
  7. some_owning_smart_ptr<foo> ptr ;
  8. ebook& operator= ( const ebook& that ) ;
  9. };
and see how much easier programming is. this technique (called RAII) is fundamental to c++ programming.
see http://en.wikipedia.org/wiki/Resourc...Initialization
and http://www.research.att.com/~bs/bs_faq2.html#memory-leaks
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 27
Reputation: rugae is an unknown quantity at this point 
Solved Threads: 0
rugae rugae is offline Offline
Light Poster

Re: deleting dynamic memory

 
0
  #4
Nov 5th, 2007
Thanks guys useful info, will put it to use after I read it couple of times...
cheers
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,857
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 120
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso

Re: deleting dynamic memory

 
0
  #5
Nov 5th, 2007
Also plan to use class in lieu of struct.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,802
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 747
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: deleting dynamic memory

 
0
  #6
Nov 5th, 2007
>Also plan to use class in lieu of struct.
That changes the semantics. class and struct are similar, but not synonymous.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC