Re: constructor and destructor

From: red floyd (no.spam_at_here.dude)
Date: 10/22/03


Date: Wed, 22 Oct 2003 08:01:07 GMT

sahukar praveen wrote:
> Hello,
>
> This is the program that I am trying. The program executes but does not give
> me a desired output.
>
> **********************************************
> #include <iostream.h>
> #include <iomanip.h>
> #include <string.h>
>
> class string{
> int length;
> char *str;
> public:
> string()
> {
> length = 0;
> str = new char [length + 1];
> strcpy(str, "\0");
> }
> string(const char *string)
> {
> cout<< "In the constructor" <<endl;
> length = strlen(string);
> str = new char [length + 1];
> strcpy (str, string);
> }
> void display (void)
> {
> cout <<"The length is : " << length<< endl;
> cout <<"The string is : " << str << endl;
> }
> ~string()
> {
> cout << "In the destructor " << length << endl;
> delete [] str;
> }
> };
>
> void main (void)
> {
> string A;
>
> A.display();
> A = string("Praveen"); //explicit call
> A.display();
> }
> ********************************************************
>
> The output that I get is
>
> The length is : 0
> The string is :
> In the constructor
> In the destructor 7
> The length is : 7
> The string is : иц
> In the destructor 7
>
>
> Now my question is
>
> 1. why is the "destructor 7" called twice? I would expect "destructor 0" to
> be called once and "destructor 7" called the next time. Am I correct?
> 2. I want to know is that line having a comment "explicit call" correct?
> Can I make a call like this?
>
> I would assume that my program has a bug (or is wrong) but I can not find
> out. Please Help.
>

1. main returns int, not void
2. I believe that the first destructor 7 call is for the temporary created by
string("Praveen")
3. You're getting garbage before the second destructor 7 because you don't have
an assignment operator.

What's happening is this:

* A is created length 0
* you display that.
* an unnamed temp is created length 7 containng "Praveen".
* You *BITWISE COPY* it to A (since you don't have an assignment operator),
meaning that A and your unnamed temp have the same value in str
* the unnamed temp is destroyed, destructor called, str is delete[] ed.
HOWEVER!!!! A.str still points to that delete[]'ed memory, and is not a valid
pointer now.
You display A, which puts out the eo garbage
You destroy A, which calls delete[] on an invalid pointe.

You need something like the following inside string:

string& operator=(const string& s)
{
     if (str != s.str)
     {
        char *temp = new char[s.length + 1];
        std::strcpy(temp,s.str);
        delete[] str;
        str = temp;
        length = s.length;
     }
     return *this;
}

Your assignments will then work properly.



Relevant Pages

  • constructor and destructor
    ... me a desired output. ... class string{ ... void main ... In the destructor 7 ...
    (comp.lang.cpp)
  • Re: Problem with resources in MFC extension DLL
    ... paths I'm storing in the string table. ... Key here is that in a destructor, especially a destructor called during program shutdown, ... particular, extension DLLs require the dynamic DLL chain be intact, and that can be ... You should avoid trying to use resources in a destructor, ...
    (microsoft.public.vc.mfc)
  • Re: stl::string memory leak associated with polymorphism
    ... only the default destructor of Base ... debug heap. ... Thus I have a 32 bytes leak per call. ... When the string "0123456789012345" is less than 16 bytes long, ...
    (microsoft.public.vc.stl)
  • Re: Problem with resources in MFC extension DLL
    ... paths I'm storing in the string table. ... It seems that you are saying that the destructor is being called *from the main program* ... You should avoid trying to use resources in a destructor, ... My program uses some classes from an extension dll. ...
    (microsoft.public.vc.mfc)
  • Re: System::String question
    ... "Ioannis Vranos" wrote in message ... we cannot create a destructor for the imported type. ... All methods exposed by the String>> class actually create a new object containing the modification. ... >>> For all types where we support the stack semantics, we require that the>> type must have a destructor or the compiler should be able to create one. ...
    (microsoft.public.dotnet.languages.vc)