Constructor and desstructor problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sweeya
    New Member
    • Nov 2008
    • 8

    Constructor and desstructor problem

    /*Program to do manipulations on a string*/

    #include <iostream>
    using namespace std;

    class String
    {
    int len;
    char *p;

    public:
    String ()
    {
    cout << "default constructor " << this << endl;
    len = 0;
    p = NULL;
    }
    String (const char *s)
    {
    cout << "String constructor called " << s << endl;
    len = strlen(s);
    p = new char[len + 1];
    strcpy(p, s);
    }
    String (const String &s)
    {
    cout << "Copy constructor invoked " << s.p << endl;;
    len = s.len;
    p = new char[len + 1];
    strcpy(p, s.p);
    }
    ~String ()
    {
    cout << "deleting " << p << " " << this << endl;
    delete p;
    }
    void display(void)
    {
    cout << p << "\t address of p = " << &p << "\tobject this = " << this << endl;
    }
    // friend String operator + (const String &s, const String &t);
    String operator + (const String &t);
    friend int operator <= (const String &s, const String &t);
    friend void show (const String &s);
    };

    String String :: operator + (const String &t)
    {
    String temp;
    cout << "Entered operator+() " << &temp << endl;

    temp.len = len + t.len;
    temp.p = new char[temp.len + 1];
    strcpy(temp.p, p);
    strcat(temp.p, t.p);

    temp.display();
    cout << "Exiting operator+() " << &temp << endl;
    return temp;
    }

    void show (const String &s)
    {
    cout << s.p;
    }

    main()
    {
    String s1("Sidharth") , s2("Sweeya ");
    String s3;

    //String s3 = s2 + s1;
    s3 = s2 + s1;

    s1.display();
    s2.display();
    s3.display();

    return 0;
    }



    Output of the program:

    String constructor called Sidharth
    String constructor called Sweeya
    default constructor 0xbff06c50
    default constructor 0xbff06c40
    Entered operator+() 0xbff06c40
    Sweeya Sidharth address of p = 0xbff06c44 object this = 0xbff06c40
    Exiting operator+() 0xbff06c40
    deleting Sweeya Sidharth 0xbff06c40
    Sidharth address of p = 0xbff06c74 object this = 0xbff06c70
    Sweeya address of p = 0xbff06c64 object this = 0xbff06c60
    address of p = 0xbff06c54 object this = 0xbff06c50
    deleting 0xbff06c50
    *** glibc detected *** double free or corruption (fasttop): 0x09e83028 ***
    Aborted (core dumped)




    If I change the code in the main to
    main()
    {
    String s1("Sidharth") , s2("Sweeya ");
    //String s3;

    String s3 = s2 + s1;
    //s3 = s2 + s1;

    s1.display();
    s2.display();
    s3.display();

    return 0;
    }


    Ouput:

    String constructor called Sidharth
    String constructor called Sweeya
    default constructor 0xbffab2b0
    Entered operator+() 0xbffab2b0
    Sweeya Sidharth address of p = 0xbffab2b4 object this = 0xbffab2b0
    Exiting operator+() 0xbffab2b0
    Sidharth address of p = 0xbffab2d4 object this = 0xbffab2d0
    Sweeya address of p = 0xbffab2c4 object this = 0xbffab2c0
    Sweeya Sidharth address of p = 0xbffab2b4 object this = 0xbffab2b0
    deleting Sweeya Sidharth 0xbffab2b0
    deleting Sweeya 0xbffab2c0
    deleting Sidharth 0xbffab2d0



    In the first case the destructor is being called for the temp variable in the operator + function. But in the second case why is not called?

    In the second case if copy constructor have been invoked then p should be pointing to a diff location. why does String s3 = s2+ s1 does not invoke a copy constructor?


    Please help me
  • svlsr2000
    Recognized Expert New Member
    • Feb 2007
    • 181

    #2
    In your function operator +, your returning "pass by value", this result in deletion of your object as soon as your function returns.

    Comment

    • Sweeya
      New Member
      • Nov 2008
      • 8

      #3
      I understand that. But why is the destructor not being called in the second case?

      i mean when i say

      String S3 = S1 + S2;

      Comment

      • Sweeya
        New Member
        • Nov 2008
        • 8

        #4
        I thought when you say

        String S3 = S2 + S1

        A copy constructor will be invoked for S3 and a destructor will be invoked for the temp obj in the operator + function

        Comment

        • svlsr2000
          Recognized Expert New Member
          • Feb 2007
          • 181

          #5
          Can you check how your deleting the array once.

          Comment

          • Sweeya
            New Member
            • Nov 2008
            • 8

            #6
            I am sorry but i did not understand what you said.

            Comment

            • svlsr2000
              Recognized Expert New Member
              • Feb 2007
              • 181

              #7
              arrays needs to deleted using delete[]ptr. But your trying to delete using delete ptr

              Comment

              • Sweeya
                New Member
                • Nov 2008
                • 8

                #8
                Hi,
                I tried deleting using delete []p. But the the output is still the same

                Comment

                • svlsr2000
                  Recognized Expert New Member
                  • Feb 2007
                  • 181

                  #9
                  can you overload = and see once, cause i am using
                  string s3(s1+s2) and it works well

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    Everyone is on the wrong track.

                    The constructors are fine, the destructors are fine and everything is being called at the right time. The delete [] is a red herring since you don't need that on arrays of built-in types.

                    What is missing is the assignment operator.

                    All S3 = S2 + S1 does is assign the char* returned from operator+ to S3. Then the destrcutor for temp is called and deletes the string held by S3.

                    Remember the Rule of the Big Three: Whenever you write a copy constructor, destructor or assignment operator, you must write all three.

                    Comment

                    • Sweeya
                      New Member
                      • Nov 2008
                      • 8

                      #11
                      Hi,
                      Thanks for the reply. Overloading = has solved the problem. But i want to understand why the destructor for temp is not called when i said

                      String S3 = S2 + S1

                      instead of

                      S3 = S2 + S1.

                      I was under the impression that in the first case the copy constructor is invoked. But it does not seem so.

                      Comment

                      • svlsr2000
                        Recognized Expert New Member
                        • Feb 2007
                        • 181

                        #12
                        weaknessforcats :- Thats why i had given an hint to sweya by saying i am using s3(s1+s2),
                        Probably she didnt get my hint.
                        Last edited by svlsr2000; Nov 30 '08, 03:40 PM. Reason: line change

                        Comment

                        • Sweeya
                          New Member
                          • Nov 2008
                          • 8

                          #13
                          HI,
                          I have tried String S3(S2 + S1) also. That did not work.

                          Comment

                          • weaknessforcats
                            Recognized Expert Expert
                            • Mar 2007
                            • 9214

                            #14
                            temp is deleted on my system regardless of String S3 = S1+ S2 or S3= S1+S2.

                            String S3 = S1 + S2 is a copy constructor call and S3 = S1 + S2 is an assignment operator call. Regalrdless, the detructor for temp is called after these calls. Be aware that the destructor call may not occur exactly after the object goes out of scope as the compiler may defer the call until later. Therefore, do not check for these destrcutor calls until the end of main().
                            Let's see your console output.

                            Comment

                            • Sweeya
                              New Member
                              • Nov 2008
                              • 8

                              #15
                              Hi,
                              I am still getting the same output and i donn see the copy constructor being invoked. While browsing through few sites, i think i found the answer to my question

                              The C++ standard explicitly allows compilers to eliminate temporary objects if the only way of detecting if those temporary objects exist is to track constructor and destructor calls.

                              A special case of this is the Return Value Optimisation (RVO), which my compiler is implementing. This optimisation basically means the compiler can avoid multiple copies if a variable within your function (eg temp within operator+()) will only be copied into a variable in the caller (eg S3 in main()).

                              Thanks all for helping me

                              Comment

                              Working...