Why Does The destructor destroy the original object??

This is a discussion on Why Does The destructor destroy the original object?? within the C++ Programming forums, part of the General Programming Boards category; Hello, this is also one of the weirdest problem that i came across and even you would agree to it..the ...

  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
    21,636
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,636
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,636
    just give me the function(copy constructor) that will solve this problem.
    You pretty much have the implementation in getdata().
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,636
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    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

Page 1 of 3 123 LastLast
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, 01:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21