I need to add a desturctor and delete/delete[] to element memory leaks.please read the attached file and code for more infromation, please check my code for solving the memory leaks and let me know what I did wrong so I can fix it ,thank you
This is a discussion on memory leaks code within the C++ Programming forums, part of the General Programming Boards category; I need to add a desturctor and delete/delete[] to element memory leaks.please read the attached file and code for more ...
I need to add a desturctor and delete/delete[] to element memory leaks.please read the attached file and code for more infromation, please check my code for solving the memory leaks and let me know what I did wrong so I can fix it ,thank you
Why are you posting code in such a virus-ridden vector as a .doc file!?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I support http://www.ukip.org/ as the first necessary step to a free Europe.
The code and details below. Looks like just another finish my homework for me question:
I need to do the following steps to the program:
Add a destructor and delete/delete[ ] as appropriate to eliminate any memory leaks.
Can change all code back to using a string rather than a c-string.
Also set pointers to null as/if needed.
Replace the formatted output method (toString) with an overloaded output/extraction operator, and modify the driver to use the overloaded operator.
Creating a copy constructor for a deep copy is probably not warranted as the title is the only object data member, but use references/pointers as needed in order to suppress the automatic invocation of the copy constructor
Here is the code:
Code:// Movie.cpp #include "Movie.h" // include Movie class definition #include <sstream> #include <string> using namespace std; Movie::Movie() { title = new char[1]; *title = '\0'; usBoxOffice = nonUSBoxOffice = worldBoxOffice = usRank = nonUSRank = worldRank = 0; } Movie::Movie(const char * temp) { istringstream iS(temp); string s; getline(iS, s, '\t'); title = new char[s.length() + 1]; strcpy(title, s.c_str()); iS >> usRank >> nonUSRank >> worldRank >> usBoxOffice >> nonUSBoxOffice >> worldBoxOffice; } const char * Movie::getTitle() const {return title;} long long Movie::getUSBoxOffice() const {return usBoxOffice;} long long Movie::getNonUSBoxOffice() const {return nonUSBoxOffice;} long long Movie::getWorldBoxOffice() const {return worldBoxOffice;} int Movie::getUSRank() const {return usRank;} int Movie::getNonUSRank() const {return nonUSRank;} int Movie::getWorldRank() const {return worldRank;} const char * Movie::toString() const { ostringstream oS; oS << "\n\n====================== Movie Information\n" << "\n Movie Title:\t" << title << "\n US Rank & Box Office:\t" << usRank << "\t$" << usBoxOffice << "\nNon-US Rank & Box Office:\t" << nonUSRank << "\t$" << nonUSBoxOffice << "\n World Rank & Box Office:\t" << worldRank << "\t$" << worldBoxOffice << "\n"; char * s = new char[500]; strcpy(s, oS.str().c_str()); return s; } // Movie.h #ifndef MOVIE_H #define MOVIE_H using namespace std; class Movie { // data is private by default char *title; long long usBoxOffice, nonUSBoxOffice, worldBoxOffice; short usRank, nonUSRank, worldRank; public: Movie() { title = new char; } ~ movie() { delete title; } Movie * returnnew () { a retval; returnt (&retval);} Movie(const char *); const char * getTitle() const; long long getUSBoxOffice() const; long long getNonUSBoxOffice() const; long long getWorldBoxOffice() const; int getUSRank() const; int getNonUSRank() const; int getWorldRank() const; const char * toString() const; }; #endif // MovieInfoApp.cpp #include "Movie.h" // include Movie class definition #include "Movies.h" // include Movies class definition #include <iostream> #include <iomanip> using namespace std; void main() { Movies movies("imdb 01-16-2011.txt"); Movie * foo = returnnew (); if(movies.getMovieCount() > 0) { char movieCode[81]; cout << "Please enter the movie search string or #number (press Enter to exit): "; cin.getline(movieCode, 80); if (strlen(movieCode) > 0) { do { const Movie * m; if(movieCode[0]=='#') m = movies.getMovie(atoi(movieCode+1)); else m = movies.getMovie(movieCode); if(m != NULL) { cout << m->toString() << "\n"; if(m->getWorldBoxOffice() > 0) cout << setprecision(2) << fixed << "\n\tUS to World Ratio: " << (m->getUSBoxOffice() * 100.0) / m->getWorldBoxOffice() << "%\n" << endl; } else cout << "\n Movie not found!\n\n" << endl; cout << "Please enter the movie search string or #number (press Enter to exit): "; cin.getline(movieCode, 80); } while (strlen(movieCode) > 0); } } } // Movies.cpp #include "Movie.h" // include Movie class definition #include "Movies.h" // include Movies class definition #include <fstream> using namespace std; Movies::Movies(const char * fn){loadMovies(fn);} int Movies::getMovieCount() const {return movieCnt;} const Movie * Movies::getMovie(const char * mc) const { if(strlen(mc)==0) return NULL; // not found else { char *mcP = myToLower(mc); int ndx=0; for(;ndx<movieCnt && (strstr(myToLower(movies[ndx].getTitle()), mcP)== NULL);ndx++); return ndx<movieCnt?&movies[ndx]:NULL; } } const Movie * Movies::getMovie(int mc) const { if(mc <= 0 || mc > movieCnt) return NULL; // not found return &movies[mc-1]; } void Movies::loadMovies(const char * fn) { ifstream iS(fn); const int LINE_LEN = 300; char s[LINE_LEN+1]; iS.getline(s, LINE_LEN); // skip heading iS.getline(s, LINE_LEN); movieCnt=0; movies = new Movie[MAX_MOVIES]; while(!iS.eof()) { movies[movieCnt++] = Movie(s); iS.getline(s, LINE_LEN); } iS.close(); reSize(); } void Movies::reSize() { Movie * m = movies; movies = new Movie[movieCnt]; for(int i=0;i<movieCnt;i++) movies[i] = m[i]; } char * Movies::myToLower(const char * s) const { int n = strlen(s); char * t = new char[n+1]; for(int i=0;i<n;i++) t[i] = tolower(s[i]); t[n] = '\0'; return t; } // Movies.h #ifndef MOVIES_H #define MOVIES_H #include "Movie.h" // include Movie class definition using namespace std; class Movies { // data is private by default static const int MAX_MOVIES = 1000; Movie *movies; Movie = newMovie [size]; delete [] movies; int movieCnt; public: Movies(const char *); int getMovieCount() const; const Movie * getMovie(const char *) const; const Movie * getMovie(int) const; private: void loadMovies(const char *); char * myToLower(const char *) const; void reSize(); }; #endif
Too ugly code. Please tag each class, cpp in separate code tags.
Try to organize in sequence.
In the end give main in separate code tag. It's really too hard to read.
I don't care if someone doesn't like me, i was not put on earth to entertain everyone.
No King, no Queen, I am the ACE of battle.
This code is terrible.
Both do not make sense, you allocate only 1 char. Use std::string.Code:title = new char; title = new char[1];
I am wondering how memory can leak code.
I never put signature, but I decided to make an exception.