0

I'm trying to understand this copy constructor problem. The question I have pertains to the destructor after the program exits. It seems the variable char* title is not destructed and I think this maybe wrong, thanks

Another question is why the assignment operator isn't called when the object x is equal to x2. I'm using g++ with codeblocks.

#include <iostream>

using namespace std;  

class myClass
{
    private:

        int x;
        int *p;
        char* title;

    public:

        myClass(int tx, int* temp, char* newtitle)
        {
            x = tx;
            cout << "int constructor called with value x = " << x << endl;

            p = new int;
            p = temp;

            title = new char [strlen(newtitle)+1];
            strcpy(title, newtitle);
        }
        myClass(const myClass& mc)
        {
            cout << "copy constructor called with value = " << mc.x << endl;
            x = mc.x;
            p = new int;
            *p = *mc.p;

            title = new char [strlen(mc.title)+1];
            strcpy(title, mc.title);
        }
        myClass & operator = (const myClass & that)
        {
            cout << "assignment called" << endl;

            if(this != &that)
            {
                x = that.x;
            }
            return *this;
        }
        ~myClass()
        {
            if (p)
            {
                cout<<"deleting p"<<endl;
                delete p;
            }
            else if(title)
            {
                cout<<"deleting title"<<endl;
                delete[] title;
            }
        }
};

int main()
{
    int pointee = 10;
    int* p = &pointee;
    myClass x(3,p,"hello");
    //myClass y = myClass(3,p);
    myClass x2 = x;
    return 0;
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
pandoragami
  • 5,387
  • 15
  • 68
  • 116
  • 3
    Maybe you need "if(title)" instead of "else if(title)" in destructor – DReJ Dec 22 '10 at 17:15
  • If you're curious: This was turned CW automatically, because too many people (including the OP) tried fixing the formatting at the same time, overwriting each others changes. – sbi Dec 22 '10 at 17:16
  • Thanks, changing the 'else if' to 'if' worked. How come the formatting is so weird on this site? – pandoragami Dec 22 '10 at 17:18
  • @lost_with_coding: The formatting is pretty simple: You just mark the text you want to format and hit the `101010` button. Can't be simpler than that. Of course, finding this out requires you to actually look at the editing help floating around beside the edit pane. – sbi Dec 22 '10 at 17:22
  • @lost_with_coding: Oh, and you're encouraged to accept an answer. This, and many other things, are explained in the FAQ, linked from the upper right of every SO page. Read it. This place works well because we stick to a few rules. If you don't know these, it won't work well for you. – sbi Dec 22 '10 at 17:24
  • @lost_with_coding: regarding your comment that "Thanks, changing the 'else if' to 'if' worked." You have a number of other problems in this code. See my post. – John Dibling Dec 22 '10 at 17:26

3 Answers3

3

You have a variety of problems that I was able to see, both in your actual code and in your general approach.

First of all, the char* title isn't deleted because you don't delete it. This is probably a logical error:

    if (p)
    {
        cout<<"deleting p"<<endl;
        delete p;
    }
    else if(title)
    {
        cout<<"deleting title"<<endl;
        delete[] title;
    }

You probably don't need the else. Why did you put it there?

Next, you are leaking an int, here:

    p = new int;
    p = temp;

The int you just new-ed gets overwritten by the passed-in value temp.

Later, you try to delete this pointer in the destructor. But since you are deleting a pointer to an automatic variable, you will hose the heap. This is also a logical error. Solution: don't do this: p = temp;

Ultimately however, your approach is questionable on multiple levels.

  1. Why do you dynamically allocate ints in the first place? Just have an int member of the class. Don't use dynamic allocation (eg new and delete) unless you really have to.
  2. Don't dynamically allocate strings using char*s. Instead, use std::string from #include <string>
  3. If you really need dynamic allocation, don't use raw pointers. Use smart pointers instead. C++ comes with one built-in, std::auto_ptr from #include <memory.h> but there are many other options, often better choices, in other libraries. A popular one here is Boost's smart pointers.
John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • Next, you are leaking an int, here: p = new int; p = temp; Should I use *p = *temp; instead? – pandoragami Dec 22 '10 at 17:41
  • @lost: No, you should not be using `temp` at all. Why do you think you need it? – John Dibling Dec 22 '10 at 17:45
  • How else would I assign an address or value to int* p? I got this as an example program from some site and I modified it a bit. – pandoragami Dec 22 '10 at 17:51
  • I got it from here http://jasonhaley.com/blog/post/2005/03/27/Thoughts-on-C2b2b-copy-constructors-the-Prototype-design-pattern-and-ICloneable-interface.aspx – pandoragami Dec 22 '10 at 17:54
  • 1
    Ok, I see. I should take source code online with a grain of salt, thanks! – pandoragami Dec 22 '10 at 17:57
  • 1
    @lost: BTW, I checked your link and the code there is not good in my opinion. I suggest you get your examples from SO, or pick up a good book: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – John Dibling Dec 22 '10 at 17:58
  • And of course "comments are closed", so I cannot point out this thread on that website. Shame. – Lightness Races in Orbit Jul 17 '11 at 16:31
1

Your destructor deletes p if p is non-NULL; and it deletes title if it's non-NULL and p is NULL.

But both your constructor and your copy constructor create new p and new title all the time. So you need to check and delete both all the time.

Chowlett
  • 45,935
  • 20
  • 116
  • 150
0

Try

*p = *temp;

instead of

 p = temp;

and in the destructor

if(title)

instead of

else if(title)
Doc Brown
  • 19,739
  • 7
  • 52
  • 88