Thread: dynamic mem allocation prgm crash

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    95

    dynamic mem allocation prgm crash

    In this prblm I am to use dynamic mem allocation for both the base class and derived class
    It compiles but crashes.

    [code]//prblm #1 pg595 C++ Primer Plus Third Edition

    #include <iostream>
    using namespace std;
    #include <stdlib.h>
    #include "classich.h"

    void Bravo(const Cd & disk);

    int main()
    {
    Cd c1("Beatles", "Capitol", 14,35.5);
    Classic c2=Classic("Piano Sonato in B flat , Fantaisia in C","Alfred Brendel","Philips",2,57.17);
    Cd * pcd = &c1;

    cout << "Using object directly:\n";
    //************************** crashes here
    pcd->Report(); // use CD method
    pcd=&c2;
    pcd->Report(); // use Classic method

    cout << "Calling a function with a CD reference argument:\n";
    Bravo(c1);
    Bravo(c2);

    cout << "Testing assignment: ";
    Classic copy;
    copy=c2;
    copy.Report();

    system("PAUSE");
    return 0;
    }

    void Bravo(const Cd & disk)
    {
    disk.Report();
    }
    [\code]

    here is the definiton file for my classes
    Code:
    //prblm #1 pg595 C++ Primer Plus Third Edition
    #include <iostream>
    using namespace std;
    #include "classich.h"
    
    Cd::Cd(char *s1,char * s2,int n,double x)
    {
     performers = new char[strlen(s1)+1];
     label = new char[strlen(s2)+1];
     strcpy(performers,s1);
     strcpy(label,s2);
     selection=n;
     playtime=x;
    }
    
    Cd::Cd(const Cd & d)
    {
     performers = new char[strlen(d.performers)+1];
     label = new char[strlen(d.label)+1];
     strcpy(performers,d.performers);
     strcpy(label,d.label);
     selection=d.selection;
     playtime=d.playtime;
    }
    
    Cd::Cd()
    {
     performers=new char[1];
     label=new char[1];
     performers[0]='\0';
     label[0]='\0';
     selection=0;
     playtime=0;
    }
    
    Cd::~Cd()
    {
     delete [] performers;
     delete [] label;
    };
    
    void Cd::Report()const
    {
     cout << "Performer:"<<performers<<endl;
     cout << "label:"<<label<<endl;
     cout << "Selection:"<<selection<<endl;
     cout << "Playtime:"<<playtime<<endl;
    }
    
    Cd & Cd::operator=(const Cd & d)
    {
     if (this==&d)
        return *this;
     delete [] performers;
     delete [] label;
     performers=new char[strlen(d.performers)+1];
     label=new char[strlen(d.label)+1];
     strcpy(performers,d.performers);
     strcpy(label,d.label);
     selection=d.selection;
     playtime=d.playtime;
     return *this;
    }
    
    
    Classic::Classic(char * p,char * s1,char * s2,int n,double x) : Cd(s1,s2,n,x)
    {
     primary=new char[strlen(p)+1];
     strcpy(primary,p);
    }
    
    Classic::Classic(const Classic & c):Cd(c)
    {
     primary=new char[strlen(c.primary)+1];
     strcpy(primary,c.primary);
    }
    
    Classic::Classic()
    {
     primary=new char[1];
     primary[0]='\0';
    }
    
    Classic::~Classic()
    {
     delete [] primary;
    }
    
    void Classic::Report() const
    {
     Cd::Report();
     cout << "Primary work:"<<primary<<endl;
    }
    
    Classic & Classic::operator=(const Classic & c)
    {
     if (this==&c)
        return *this;
     Cd::operator=(c);
     delete [] primary;
     primary=new char[strlen(c.primary)+1];
     strcpy(primary,c.primary);
     return *this;
    }
    I can't spot the prblm please help!

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    I just scanned your code but I noticed that you aren't setting the pointers to NULL before trying to reallocate them. I'm going to go through it in more detail and I'll let you know if I find anything else.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    95
    In my copy constructors you mean?

  4. #4
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Whenever you use delete to remove dynamically allocated pointer created with new you should set the pointer to NULL.
    Like this:
    Code:
    //single pointer
    int i;
    i=new int;
    delete i;
    i=NULL;
    
    //for arrays
    int i;
    i=new int[5];
    delete [] i;
    i=NULL;

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    95
    I updated the code to set the pointers to NULL like you said. My book says nothing about this. Anyhow it still crashes in the same place.

    Also i set the pointers to NULL after deleting them in the destructor. Is this neccesary as well?

    I attempted with and with out neither fixed the crash.

    Thanks for helping me out here.

  6. #6
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Ok. I'll look at your code a little bit more and let you know if I find anything.

  7. #7
    Registered User Liam Battle's Avatar
    Join Date
    Jan 2002
    Posts
    114
    run your code through the debugger and tell us the last working line...

    then you would have this solved in 5 seconds...
    LB0: * Life once school is done
    LB1: N <- WakeUp;
    LB2: N <- C++_Code;
    LB3: N >= Tired : N <- Sleep;
    LB4: JMP*-3;

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    95
    I'm using Dev-C++ but do not know how to get the debuger working. When I select debug nothing happens.
    The last working line seem to be

    cout << "Using object directly:\n";
    //************************** crashes here
    pcd->Report(); // use CD method

    in main.

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    49
    probably you did not declare your Report() funtion as virtual?
    Perhaps ...
    Let's know your header file is the key.(Is there any grammar error with this sentence? )
    Hello, everyone.

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by rip1968
    I updated the code to set the pointers to NULL like you said. My book says nothing about this. Anyhow it still crashes in the same place.

    Also i set the pointers to NULL after deleting them in the destructor. Is this neccesary as well?

    I attempted with and with out neither fixed the crash.

    Thanks for helping me out here.
    Well, no errors will occur from not setting pointers to null after deleting them. This is done as a precaution so if you try and dereference the pointer later in the program and you are unaware, the program will certainly crash.

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    95
    Here is the prototypes for the base class.
    Report is declared virtual. I appreciate all your comments. I never cease to learn from this board.

    Code:
    //prblm #1 pg595 C++ Primer Plus Third Edition
    #ifndef _CDH_H_
    #define _CDH_H_
    
    class Cd
    {
     private:
             char * performers;
             char * label;
             int selection;      
             double playtime;
     public:
            Cd(char *s1,char * s2,int n,double x);
            Cd(const Cd & d);
            Cd();
            virtual ~Cd();
            virtual void Report()const;
            Cd & operator=(const Cd & d);
    };
    #endif

  12. #12
    Registered User
    Join Date
    Apr 2002
    Posts
    95
    Thanks you all for the information concerning setting pointers to NULL after they are deleted. The program is now working. I believe it had to do with the object code for my main file not being updated when I compiled the source code. When I made a slight change to the file it did a frish compile of the source code. Next time I will use the rebuild all option doh

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic allocation from 1 instead of zero
    By cfdprogrammer in forum C Programming
    Replies: 27
    Last Post: 04-28-2009, 08:21 AM
  2. pointer to array with dynamic allocation
    By cfdprogrammer in forum C Programming
    Replies: 22
    Last Post: 04-07-2009, 09:56 AM
  3. Dynamic mem allocation, how to get it right?
    By Subsonics in forum C Programming
    Replies: 31
    Last Post: 02-08-2009, 01:41 PM
  4. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  5. Dynamic allocation (I thought it would crash)
    By Baaaah! in forum C Programming
    Replies: 16
    Last Post: 11-30-2005, 05:10 PM