Missing Destructor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AB

    Missing Destructor

    Hi All,

    I've got an array of objects, during the execution of the program I'd
    like to assign a particular object to a certain element in the object
    array. The sample code's like this...

    class ClassA
    {
    public:
    ClassA()
    : m(rand() % 173) { cout<<"\nRandom Constructor: "<<m ; }

    ClassA(int val)
    : m(val) { cout<<"\nConstr ucted: "<<m ; }

    ~ClassA() { cout<<"\n\nDest ructor: "<<m ; }
    private:
    int m ;
    } ;

    int main()
    {
    srand((unsigned )time(NULL)) ;

    const int size = 3 ;
    ClassA *c = new ClassA[size] ;

    c[0] = ClassA(200) ;
    c[0] = ClassA(201) ;

    delete[] c ;
    return 0 ;
    }

    //Output...
    Random Constructor: 50 <--- (object vanishes without destructor call)
    Random Constructor: 38
    Random Constructor: 148
    Constructed: 200

    Destructor: 200
    Constructed: 201

    Destructor: 201 <--- (1. 1st destructor call for this object)

    Destructor: 148

    Destructor: 38

    Destructor: 201 <--- (2. object is being destroyed twice or has it
    replaced the very first object?)

    As I've pointed out in the output, the the destructor for the 1st
    object never gets called, whereas the destructor for the object created
    with data = 201 gets called twice. Is this the case or is it the very
    first object which is in fact getting destroyed in (2) ?

    Also, what's the best practice for such a situation?

  • Jim Langston

    #2
    Re: Missing Destructor


    "AB" <abjbhat@gmail. com> wrote in message
    news:1151557214 .393516.69970@i 40g2000cwc.goog legroups.com...[color=blue]
    > Hi All,
    >
    > I've got an array of objects, during the execution of the program I'd
    > like to assign a particular object to a certain element in the object
    > array. The sample code's like this...
    >
    > class ClassA
    > {
    > public:
    > ClassA()
    > : m(rand() % 173) { cout<<"\nRandom Constructor: "<<m ; }
    >
    > ClassA(int val)
    > : m(val) { cout<<"\nConstr ucted: "<<m ; }
    >
    > ~ClassA() { cout<<"\n\nDest ructor: "<<m ; }
    > private:
    > int m ;
    > } ;
    >
    > int main()
    > {
    > srand((unsigned )time(NULL)) ;
    >
    > const int size = 3 ;
    > ClassA *c = new ClassA[size] ;
    >
    > c[0] = ClassA(200) ;
    > c[0] = ClassA(201) ;
    >
    > delete[] c ;
    > return 0 ;
    > }
    >
    > //Output...
    > Random Constructor: 50 <--- (object vanishes without destructor call)
    > Random Constructor: 38
    > Random Constructor: 148
    > Constructed: 200
    >
    > Destructor: 200
    > Constructed: 201
    >
    > Destructor: 201 <--- (1. 1st destructor call for this object)
    >
    > Destructor: 148
    >
    > Destructor: 38
    >
    > Destructor: 201 <--- (2. object is being destroyed twice or has it
    > replaced the very first object?)
    >
    > As I've pointed out in the output, the the destructor for the 1st
    > object never gets called, whereas the destructor for the object created
    > with data = 201 gets called twice. Is this the case or is it the very
    > first object which is in fact getting destroyed in (2) ?
    >
    > Also, what's the best practice for such a situation?[/color]

    All I can say is I see the same behavior in MS VC++ .net 2003




    Comment

    • Vikram

      #3
      Re: Missing Destructor


      AB wrote:[color=blue]
      > Hi All,
      >
      > I've got an array of objects, during the execution of the program I'd
      > like to assign a particular object to a certain element in the object
      > array. The sample code's like this...
      >
      > class ClassA
      > {
      > public:
      > ClassA()
      > : m(rand() % 173) { cout<<"\nRandom Constructor: "<<m ; }
      >
      > ClassA(int val)
      > : m(val) { cout<<"\nConstr ucted: "<<m ; }
      >
      > ~ClassA() { cout<<"\n\nDest ructor: "<<m ; }
      > private:
      > int m ;
      > } ;
      >
      > int main()
      > {
      > srand((unsigned )time(NULL)) ;
      >
      > const int size = 3 ;
      > ClassA *c = new ClassA[size] ;
      >
      > c[0] = ClassA(200) ;
      > c[0] = ClassA(201) ;
      >
      > delete[] c ;
      > return 0 ;
      > }
      >
      > //Output...
      > Random Constructor: 50 <--- (object vanishes without destructor call)
      > Random Constructor: 38
      > Random Constructor: 148
      > Constructed: 200
      >
      > Destructor: 200
      > Constructed: 201
      >
      > Destructor: 201 <--- (1. 1st destructor call for this object)
      >
      > Destructor: 148
      >
      > Destructor: 38
      >
      > Destructor: 201 <--- (2. object is being destroyed twice or has it
      > replaced the very first object?)
      >
      > As I've pointed out in the output, the the destructor for the 1st
      > object never gets called, whereas the destructor for the object created
      > with data = 201 gets called twice. Is this the case or is it the very
      > first object which is in fact getting destroyed in (2) ?
      >
      > Also, what's the best practice for such a situation?[/color]

      A statement like
      c[0] = ClassA(200) ;
      constructs a temporary object and assigns it to c[0]. After the
      statement the temporary object is destructed and not c[0]. That is the
      reason you see contructed and destructed for 200. They refer to temp
      object rather than c[0].

      Comment

      • Alf P. Steinbach

        #4
        Re: Missing Destructor

        * AB:[color=blue]
        > Hi All,
        >
        > I've got an array of objects, during the execution of the program I'd
        > like to assign a particular object to a certain element in the object
        > array. The sample code's like this...
        >
        > class ClassA
        > {
        > public:
        > ClassA()
        > : m(rand() % 173) { cout<<"\nRandom Constructor: "<<m ; }
        >
        > ClassA(int val)
        > : m(val) { cout<<"\nConstr ucted: "<<m ; }
        >
        > ~ClassA() { cout<<"\n\nDest ructor: "<<m ; }
        > private:
        > int m ;
        > } ;
        >
        > int main()
        > {
        > srand((unsigned )time(NULL)) ;
        >
        > const int size = 3 ;
        > ClassA *c = new ClassA[size] ;
        >
        > c[0] = ClassA(200) ;
        > c[0] = ClassA(201) ;
        >
        > delete[] c ;
        > return 0 ;
        > }
        >
        > //Output...
        > Random Constructor: 50 <--- (object vanishes without destructor call)
        > Random Constructor: 38
        > Random Constructor: 148
        > Constructed: 200
        >
        > Destructor: 200
        > Constructed: 201
        >
        > Destructor: 201 <--- (1. 1st destructor call for this object)
        >
        > Destructor: 148
        >
        > Destructor: 38
        >
        > Destructor: 201 <--- (2. object is being destroyed twice or has it
        > replaced the very first object?)
        >
        > As I've pointed out in the output, the the destructor for the 1st
        > object never gets called, whereas the destructor for the object created
        > with data = 201 gets called twice. Is this the case or is it the very
        > first object which is in fact getting destroyed in (2) ?
        >
        > Also, what's the best practice for such a situation?[/color]

        What I call the "C++ construction guarantee": except for use of very
        low-level features each object receives exactly one constructor call
        (after all subobjects constructed), which is the first thing that
        happens for that object, and for each constructor call there is at most
        one destructor call -- unless you fail to destroy an object there is
        exactly one destructor call for each constructor call.

        The code above uses two techniques to make it less apparent what's going on:

        * Value assignment that changes the apparent identity of an object.

        * Temporary objects.

        You first destructor call reporting id 201 is of the first array
        element, the second is of the temporary object, which has the same id.

        To investigate constructor and destructor calls you need code that is
        easy to analyze instead of code that brings in all kinds of effects.

        --
        A: Because it messes up the order in which people normally read text.
        Q: Why is it such a bad thing?
        A: Top-posting.
        Q: What is the most annoying thing on usenet and in e-mail?

        Comment

        • AB

          #5
          Re: Missing Destructor

          >Alf P. Steinbach wrote:[color=blue]
          >You first destructor call reporting id 201 is of the first array
          >element, the second is of the temporary object, which has the same id.[/color]

          I thought it was the other way around. First the temporary object with
          id 201 would be destroyed and the array's first object with id 201 last.

          Comment

          • Rolf Magnus

            #6
            Re: Missing Destructor

            AB wrote:
            [color=blue][color=green]
            >>Alf P. Steinbach wrote:
            >>You first destructor call reporting id 201 is of the first array
            >>element, the second is of the temporary object, which has the same id.[/color]
            >
            > I thought it was the other way around. First the temporary object with
            > id 201 would be destroyed and the array's first object with id 201 last.[/color]

            I'd say you're right. The temporary is destroyed as soon as the assignment
            is finished. The array is destroyed later.

            Comment

            • Alf P. Steinbach

              #7
              Re: Missing Destructor

              * Rolf Magnus:[color=blue]
              > AB wrote:
              >[color=green][color=darkred]
              >>> Alf P. Steinbach wrote:
              >>> You first destructor call reporting id 201 is of the first array
              >>> element, the second is of the temporary object, which has the same id.[/color]
              >> I thought it was the other way around. First the temporary object with
              >> id 201 would be destroyed and the array's first object with id 201 last.[/color]
              >
              > I'd say you're right. The temporary is destroyed as soon as the assignment
              > is finished. The array is destroyed later.[/color]

              Yes, that seems to be the case here.

              --
              A: Because it messes up the order in which people normally read text.
              Q: Why is it such a bad thing?
              A: Top-posting.
              Q: What is the most annoying thing on usenet and in e-mail?

              Comment

              • Zara

                #8
                Re: Missing Destructor

                On 28 Jun 2006 22:00:14 -0700, "AB" <abjbhat@gmail. com> wrote:
                [color=blue]
                >Hi All,
                >
                >I've got an array of objects, during the execution of the program I'd
                >like to assign a particular object to a certain element in the object
                >array. The sample code's like this...
                >[/color]
                <....>

                You may see everything more clearly if you add a copy assignment
                operator:

                class ClassA {
                /* All your functions here */
                ClassA& operator=(const ClassA& anotherInstance ) {
                cout<<"\nCopy Assigned, changes from "<<m
                <<" to "<<anotherInsta nce.m;
                m=anotherInstan ce.m;
                }
                };

                "One image is better than 1000 words"

                Regards,

                Zara

                Comment

                • Jim Langston

                  #9
                  Re: Missing Destructor


                  "Alf P. Steinbach" <alfps@start.no > wrote in message
                  news:4ghg21F1mu 8hjU1@individua l.net...[color=blue]
                  >* Rolf Magnus:[color=green]
                  >> AB wrote:
                  >>[color=darkred]
                  >>>> Alf P. Steinbach wrote:
                  >>>> You first destructor call reporting id 201 is of the first array
                  >>>> element, the second is of the temporary object, which has the same id.
                  >>> I thought it was the other way around. First the temporary object with
                  >>> id 201 would be destroyed and the array's first object with id 201 last.[/color]
                  >>
                  >> I'd say you're right. The temporary is destroyed as soon as the
                  >> assignment
                  >> is finished. The array is destroyed later.[/color]
                  >
                  > Yes, that seems to be the case here.[/color]

                  The question remains however. Why do we not see a Drestructor for 50?

                  //Output...
                  Random Constructor: 50 <--- (object vanishes without destructor call)
                  Random Constructor: 38
                  Random Constructor: 148
                  Constructed: 200

                  Destructor: 200
                  Constructed: 201

                  Destructor: 201 <--- (1. 1st destructor call for this object)

                  Destructor: 148

                  Destructor: 38

                  Destructor: 201 <--- (2. object is being destroyed twice or has it
                  replaced the very first object?)


                  Comment

                  • Alf P. Steinbach

                    #10
                    Re: Missing Destructor

                    * Jim Langston:[color=blue]
                    >
                    > The question remains however. Why do we not see a Drestructor for 50?
                    >[/color]

                    Quoting myself:
                    [color=blue]
                    > Value assignment that changes the apparent identity of an object.[/color]

                    --
                    A: Because it messes up the order in which people normally read text.
                    Q: Why is it such a bad thing?
                    A: Top-posting.
                    Q: What is the most annoying thing on usenet and in e-mail?

                    Comment

                    • Rolf Magnus

                      #11
                      Re: Missing Destructor

                      Jim Langston wrote:
                      [color=blue]
                      >
                      > "Alf P. Steinbach" <alfps@start.no > wrote in message
                      > news:4ghg21F1mu 8hjU1@individua l.net...[color=green]
                      >>* Rolf Magnus:[color=darkred]
                      >>> AB wrote:
                      >>>
                      >>>>> Alf P. Steinbach wrote:
                      >>>>> You first destructor call reporting id 201 is of the first array
                      >>>>> element, the second is of the temporary object, which has the same id.
                      >>>> I thought it was the other way around. First the temporary object with
                      >>>> id 201 would be destroyed and the array's first object with id 201
                      >>>> last.
                      >>>
                      >>> I'd say you're right. The temporary is destroyed as soon as the
                      >>> assignment
                      >>> is finished. The array is destroyed later.[/color]
                      >>
                      >> Yes, that seems to be the case here.[/color]
                      >
                      > The question remains however. Why do we not see a Drestructor for 50?[/color]

                      Because you copied the object with id 201 to it, so it now has id 201, too.
                      What did you expect to happen with the object when assigning another object
                      to it?

                      Comment

                      • Jim Langston

                        #12
                        Re: Missing Destructor

                        "Rolf Magnus" <ramagnus@t-online.de> wrote in message
                        news:e80735$d6r $02$1@news.t-online.com...[color=blue]
                        > Jim Langston wrote:
                        >[color=green]
                        >>
                        >> "Alf P. Steinbach" <alfps@start.no > wrote in message
                        >> news:4ghg21F1mu 8hjU1@individua l.net...[color=darkred]
                        >>>* Rolf Magnus:
                        >>>> AB wrote:
                        >>>>
                        >>>>>> Alf P. Steinbach wrote:
                        >>>>>> You first destructor call reporting id 201 is of the first array
                        >>>>>> element, the second is of the temporary object, which has the same
                        >>>>>> id.
                        >>>>> I thought it was the other way around. First the temporary object with
                        >>>>> id 201 would be destroyed and the array's first object with id 201
                        >>>>> last.
                        >>>>
                        >>>> I'd say you're right. The temporary is destroyed as soon as the
                        >>>> assignment
                        >>>> is finished. The array is destroyed later.
                        >>>
                        >>> Yes, that seems to be the case here.[/color]
                        >>
                        >> The question remains however. Why do we not see a Drestructor for 50?[/color]
                        >
                        > Because you copied the object with id 201 to it, so it now has id 201,
                        > too.
                        > What did you expect to happen with the object when assigning another
                        > object
                        > to it?[/color]

                        Wasn't me, but someone else. Yes, now I understand. No destructor for
                        assignment.


                        Comment

                        Working...