Thread: Why Does The destructor destroy the original object??

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    Why Does The destructor destroy the original object??

    Hello,
    this is also one of the weirdest problem that i came across and even you would agree to it..the code is

    Code:
    #include <string.h>
    #include <iostream.h>
    
    class str
    {
    	char *s;
    	public:
    		~str()
    		{
    			delete s;
    		}
    		void getdata(char*);
    		void putdata();
    		friend int xstrlen(str);
    
    };
    int xstrlen(str A)
    {
     int l=0;
     while(*A.s)
     {
    	l++;
    	A.s++;
     }
     return l;
    }
    
    void str::getdata(char* p)
    {
    	s=new char[strlen(p)+1];
    	strcpy(s,p);
    }
    void str::putdata()
    {
    	cout<<s<<endl;
    }
    
    int main(void)
    {
     
     str A;
     A.getdata("string");
     A.putdata();
    
     xstrlen(A);
    
     A.putdata();
    
    
     return 0;
    }
    Ok the 2nd A.putdata() prints garbage because when we come out of xstrlen the destructor gets called and destroys the object. That's fine but why does it destroy the original A object even though i have used call by value?? can anybody answer this to me??

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That's fine but why does it destroy the original A object even though i have used call by value?
    It does that precisely because of call/pass by value. The object named A in xstrlen() is a copy of the object named A in main().

    Kindly indent your code properly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    ya that's what i mean to say. If it's copy that means the original object shouldn't get modified?then why is it getting modified?why is the original object getting destroyed?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If it's copy that means the original object shouldn't get modified?
    Yes.

    then why is it getting modified?why is the original object getting destroyed?
    However, you have char* member, and this pointer is copied. So, even though the original str object is not modified, the null terminated string that it points to was modified.

    In other words, you need to provide a copy constructor and copy assignment operator that performs deep copying. Incidentally, your destructor is wrong as it should delete[], not delete.

    If you were not writing a string class, using an existing string class like std::string would be best.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Ok thanks laserlight but i m not aware about it can you please write that for me?pleaseeee
    just give me the function(copy constructor) that will solve this problem.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    just give me the function(copy constructor) that will solve this problem.
    You pretty much have the implementation in getdata().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    This is not supposed to be my homework still why are you giving me answers in round about manner?

    can anybody else help me out?and write a copy constructor which does the job?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is not supposed to be my homework still why are you giving me answers in round about manner?
    You have made it your homework, so give it a try first.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It doesn't matter if it's homework or not, simply giving a solution will not help you. But I can help you get started.
    A copy constructor is a constructor that takes a reference to an object of its own type to copy from. So in your case, it will look like:

    Definition:
    str(const str& rCopy);

    Implementation:
    str::str(const str& rCopy)

    In the copy constructor, you have to make sure to create a copy of your string, just like getdata does.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Because Laserlight [rightly] tries to teach you to solve your own problems, not learn how to get them "off the net".

    A copy constructor is needed when you have allocated data in the class that gets deallocated in the destructor, as you have seen.

    The default copy constructor is essentially this:
    Code:
    X(const X &x)
    {
        memcpy(this, &x, sizeof(X));
    }
    But you need to create a new string in your copy constructor, otherwise the original pointer to the string is destroyed in the destructor.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Ok is this solution correct??

    Code:
    #include <string.h>
    #include <iostream.h>
    
    class str
    {
    	char *s;
    	public:
    		str(){}
    		~str()
    		{
    			delete []s;
    		}
    		str(str &p)
    		{
    			memcpy(this,&s,sizeof(s));
    			s=new char[strlen(p.s)+1];
    		}
    		void getdata(char*);
    		void putdata();
    		friend int xstrlen(str);
    
    };
    int xstrlen(str A)
    {
     int l=0;
     while(*A.s)
     {
    	l++;
    	A.s++;
     }
     return l;
    }
    
    void str::getdata(char* p)
    {
       s=new char[strlen(p)+1];
       strcpy(s,p);
    
    }
    void str::putdata()
    {
    	cout<<s<<endl;
    }
    
    int main(void)
    {
     str A;
     A.getdata("string");
     A.putdata();
    
    
     xstrlen(A);
     A.putdata();
    
     return 0;
    }
    if yes! then now could you please explain me what am i doing when i call the xstrlen()??the control passes to copy constructor?but why??that i dont know?now please anybody explain me!what exactly is happening?
    Last edited by chottachatri; 03-12-2008 at 07:54 AM.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    1) No, it's not correct. You are copying uninitialized data into an uninitialized pointer.
    You must first allocate data using new. Then you should copy p.s (the copy's string), and not s itself, because it belongs to your own object which is being constructed.
    2) getdata should possible return the string, and not set it. Misleading name.
    3) You're passing by value, which means a new object is created when passed to the function. Everytime you do a copy of an object, its copy constructor is called.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    int xstrlen(str A)
    Since the argument for xstrlen() is not a reference, it requires the compiler to make a copy of the incoming argument. And to do that, it needs to call the copy constructor.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As Elysia hinted, your copy constructor isn't correct:
    Code:
    		str(str &p)
    		{
    			memcpy(this,&s,sizeof(s));
    			s=new char[strlen(p.s)+1];
    		}
    The red code is not needed [and incorrect]
    The green code is correct, but you would probably need to do more than just allocate the data space.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    ok fine..let me try more

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Object destroy itself?
    By cminusminus in forum C++ Programming
    Replies: 28
    Last Post: 03-27-2008, 01:08 AM
  2. GDI object lifetime and C++ object lifetime
    By VirtualAce in forum Windows Programming
    Replies: 4
    Last Post: 06-16-2006, 05:26 AM
  3. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  4. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 13
    Last Post: 10-31-2002, 02:56 PM