| | |
deleting dynamic memory
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2007
Posts: 27
Reputation:
Solved Threads: 0
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?
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?
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.
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
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
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
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:
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:
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
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
C++ Syntax (Toggle Plain Text)
struct book { book( const char* t, const char* a ) ; ~book() ; // required book( const book& that ) ; // required book& operator= ( const book& that ) ; // required char* title ; char* author ; }; book::book( const char* t, const char* a ) { size_t title_len = strlen(t) ; title = new char[ title_len+1 ] ; strcpy( title, t ) ; size_t auth_len = strlen(a) ; author = new char[ auth_len+1 ] ; // this may fail (throw std::bad_alloc) // if it does, memory for title will leak. note: destructor of the book // being constructed will not (and should not) be called as the constructor // has not completed. so we have to scaffold this in a try/catch block. strcpy( author, a ) ; }
C++ Syntax (Toggle Plain Text)
struct foo { /* ... */ } ; struct ebook : public book { /* ... */ foo* ptr ; // ebook 'owns' object pointed to by ptr ebook& operator= ( const ebook& that ) ; // implement this correctly };
now compare this with
C++ Syntax (Toggle Plain Text)
struct book { book( const char* t, const char* a ) ; // compiler generated destructor, copy constructor, assignment std::string title ; std::string author ; }; book::book( const char* t, const char* a ) : title(t), author(a) { // constructor of author may fail (throw std::bad_alloc) // if it does, title, which has already been constructed will be // destroyed during the stack unwind; we have nothing to worry about. }
C++ Syntax (Toggle Plain Text)
struct foo { /* ... */ } ; struct ebook : public book { /* ... */ some_owning_smart_ptr<foo> ptr ; ebook& operator= ( const ebook& that ) ; };
see http://en.wikipedia.org/wiki/Resourc...Initialization
and http://www.research.att.com/~bs/bs_faq2.html#memory-leaks
Also plan to use class in lieu of struct.
![]() |
Similar Threads
- help with dynamic memory (C++)
- Dynamic memory for an array of objects (C++)
- Static and Dynamic Memory Allocations...Advan's & Disadvan's??? (Computer Science)
- regarding dynamic memory allocation (C)
- Dynamic memory allocation homework (C++)
Other Threads in the C++ Forum
- Previous Thread: Regarding Env variables.
- Next Thread: breaking sentence into sub string
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference rpg simple string strings system temperature template templates test text text-file tree unix url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






