Thread: memory leaks code

  1. #1
    Registered User
    Join Date
    Mar 2011

    memory leaks code

    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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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.

  3. #3
    Join Date
    Dec 2007
    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:

    // 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;
    	{ 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;
    // 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;
    					m = movies.getMovie(atoi(movieCode+1));
    					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 {
    		return NULL; // not found
    	else {
    		char *mcP = myToLower(mc);
    		int ndx=0;
    		for(;ndx<movieCnt &&
    			(strstr(myToLower(movies[ndx].getTitle()), mcP)==
    		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);
    	movies = new Movie[MAX_MOVIES];
    	while(!iS.eof()) {
    		movies[movieCnt++] = Movie(s);
    		iS.getline(s, LINE_LEN);
    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;
    	Movies(const char *);
    	int getMovieCount() const;
    	const Movie * getMovie(const char *) const;
    	const Movie * getMovie(int) const;
    	void loadMovies(const char *);
    	char * myToLower(const char *) const;
    	void reSize();

  4. #4
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Middle of NoWhere
    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.

  5. #5
    Registered User
    Join Date
    Aug 2010
    This code is terrible.
    title = new char;
    title = new char[1];
    Both do not make sense, you allocate only 1 char. Use std::string.

    I am wondering how memory can leak code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cygwin on win64
    By Vanzemljak in forum Tech Board
    Replies: 3
    Last Post: 01-12-2011, 04:28 PM
  2. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  3. Linking error with memory leak detection code. HELP!
    By Lister in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2003, 03:54 PM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM