C++: destructor and delete

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

    #31
    Re: C++: destructor and delete

    Newsnet Customer wrote:
    [color=blue]
    > ...
    > Statement 1: "A dynamically created local object will call it's destructor
    > method when it goes out of scope when a procedure returms"
    >
    > Agree.[/color]

    The question makes no sense. Local objects, by definition, cannot be
    dynamically created in C++. Local object can be either automatic or
    static. Actually, the standard is rather ambiguous on whether locally
    defined static objects can be referred to as "local", but in no case a
    dynamically created object can be referred to as "local".
    [color=blue]
    >
    > Statement 2: "A dynamically created object will call it's destructor when it
    > is made a target of a delete".[/color]

    This question also makes no sense. Nowhere in C++ specification it says
    that the object is responsible for calling its own destructor. The
    destructor is called when the object is destroyed. Who performs the call
    - no one knows.

    OK, let's assume that the object being destroyed calls its own destructor.
    [color=blue]
    > Does not make sense to me. Would not this result in an endless loop? (ie.
    > delete calls destructor and within destructor it calls delete, which in turn
    > calls the destructor again and so on....)[/color]

    No there's no endless loop here. Object's destructor doesn't call
    'delete'. What makes you think it does?

    --
    Best regards,
    Andrey Tarasevich
    Brainbench C and C++ Programming MVP

    Comment

    • Andrey Tarasevich

      #32
      Re: destructor and delete

      Newsnet Customer wrote:
      [color=blue][color=green]
      >> Sometimes you delete a subobject in the destructor. You don't delete the
      >> original object.
      >>
      >> class X
      >> {
      >> ~X()
      >> {
      >> delete ptr; // delete Y object, calls Y destructor
      >> }
      >> Y* ptr;
      >> };
      >>
      >> X* p = new X;
      >> delete p; // deletes an X object, calls X destructor
      >>
      >> The only way there would be a loop was if you did 'delete this' in the
      >> destructor.
      >>
      >> john[/color]
      >
      > If you perform a delete on a object, which then calls it's destructor
      > method, and this method is simply empty. How is the original object actually
      > being deleted? I can only assume the delete operator calls the destructor
      > method and then deletes the original object (outside of the destructor),
      > which you don't explicitly do yourself.
      > ...[/color]

      If the destructor method is really "simply empty" then the only thing
      you need to do to destroy the object is to free the memory it occupies.
      That's exactly what delete-expression does after calling the object's
      destructor: it frees the memory by calling the appropriate function
      'operator delete'. Where exactly do you see the endless loop here?

      --
      Best regards,
      Andrey Tarasevich
      Brainbench C and C++ Programming MVP

      Comment

      • Andrey Tarasevich

        #33
        Re: destructor and delete

        Newsnet Customer wrote:
        [color=blue][color=green][color=darkred]
        >> > Destructor would be called only for the object created on the heap.[/color]
        >>
        >> I meant stack ofcourse ;-).[/color]
        >
        > incorrect. as indicated in statement 1. an object's (dynamically created
        > ...meaning HEAP) destructor is called when the object goes out of scope.
        > ...[/color]

        The first statement in your original post does not indicate anything. It
        is terminologicall y incorrect and, therefore, it is completely meaningless.

        --
        Best regards,
        Andrey Tarasevich
        Brainbench C and C++ Programming MVP

        Comment

        • Andrey Tarasevich

          #34
          Re: destructor and delete

          Newsnet Customer wrote:[color=blue]
          > ...
          > 1) dynamically created objects do not call their destructors when they go
          > out of scope - only for objects on the stack. OR[/color]

          C++ has no concept of "objects calling their destructors", no matter
          what these objects are: static, local, dynamic, "on stack", etc.
          [color=blue]
          > 2) default destructor of pointers to dynamically created objects are called
          > when they go out of scope.[/color]

          Firstly, C++ has no concept of "default destructor". Secondly, pointers
          are object of non-class types. The have no destructors at all. Nothing
          is called.

          --
          Best regards,
          Andrey Tarasevich
          Brainbench C and C++ Programming MVP

          Comment

          • John Harrison

            #35
            Re: destructor and delete

            >[color=blue]
            > Tried it, and you are right.
            >
            > Either:
            >
            > 1) dynamically created objects do not call their destructors when they go
            > out of scope - only for objects on the stack. OR
            > 2) default destructor of pointers to dynamically created objects are[/color]
            called[color=blue]
            > when they go out of scope.
            >
            > So is it 1 or 2? who knows.
            >[/color]

            1 is closer to the truth, but you have to get clear in your head the
            difference between the pointer to the dynamic object, and the object itself.

            The pointer does go out of scope because it is on the stack, but the pointer
            going out of scope has no effect at all on the dynamic object.

            The dynamically created object never goes out of scope, it has no scope, it
            only becomes invalid when it is deleted.

            john


            Comment

            • Kevin Goodsell

              #36
              Re: destructor and delete

              Newsnet Customer wrote:
              [color=blue]
              >
              >
              > I don't just accept everything people tell me; Do you? of course not. I just
              > wasn't convinced by your argument and others.[/color]

              So you're just here to waste our time by arguing with people who clearly
              know far more about the language than you do?

              This is not a philosophical debate. There's no room for argument. There
              is One Right Answer, which you were given *several* times, and ignored.
              You are seeming more trollish with every post.

              -Kevin
              --
              My email address is valid, but changes periodically.
              To contact me please use the address from a recent posting.

              Comment

              • llewelly

                #37
                Re: destructor and delete

                "Newsnet Customer" <nistygravy@ipr imus.com.au> writes:
                [color=blue][color=green][color=darkred]
                >> >
                >> >
                >> >
                >> > > > Destructor would be called only for the object created on the heap.
                >> > >
                >> > > I meant stack ofcourse ;-).
                >> >
                >> > incorrect. as indicated in statement 1. an object's (dynamically created
                >> > ...meaning HEAP) destructor is called when the object goes out of scope.
                >> >[/color]
                >>
                >> No it is not.
                >>
                >> {
                >> X* ptr = new X;
                >> }
                >>
                >> No destructors are called in the above code, because POINTERS do not have
                >> destructors. ptr is a POINTER to X, its is not an X.[/color]
                >
                > The delete operator is only performed on pointers, so it calls the
                > destructor method of the object it's pointing to. In your case, the
                > destructor of X is called when the procedure returns.
                >
                >[color=green]
                >> {
                >> X x;
                >> }
                >>
                >> X destructor is called (assuming it has one).[/color]
                >
                > An object will always have a constructor and a destructor.[/color]

                Not if the author of the class declares one and does not define it.
                [color=blue]
                > If either is not defined,[/color]

                No. If either is not *declared* ...
                [color=blue]
                > they will be created for you
                > implicitly.[/color]

                This class has no destructor.

                struct has_no_destruct or
                {
                private:
                ~has_no_destruc tor();
                };

                The same can be done with constructors and copy-assignment operators.

                [color=blue]
                > Since pointers are objects too, they will have a constructor and
                > destructor.[/color]

                built-in types have 'trivial destructors'. (That's what the standard
                calls them.) Note to other posters - nothing in the standard requires
                they 'don't do anything' - it would be permissible for int's
                destructor to set the bytes it occupied to '0xDEADBEEF' or
                '0xCDCDCDCD' - and for a debugging implementation of C++, that
                would even be desirable. The standard does define 'trivial
                destructors' in such a way as to *allow* a trivial destructor to
                do nothing, but there's no requirement.

                Comment

                • Ron Natalie

                  #38
                  Re: destructor and delete


                  "llewelly" <llewelly.at@xm ission.dot.com> wrote in message news:86isobdp9h .fsf@Zorthluthi k.local.bar...
                  [color=blue][color=green]
                  > > Since pointers are objects too, they will have a constructor and
                  > > destructor.[/color]
                  >
                  > built-in types have 'trivial destructors'. (That's what the standard
                  > calls them.)[/color]

                  No, sorrry. They don't and the standard doesn't call them that. Trivial constructors
                  exist only for class type. Trivial constructors are a subset of the implicitly declared
                  default constructor which is also only exists for classes.

                  Non-classes don't have constructors or destructors as they don't have member
                  functions at all. You can (mostly for the convenience of template program),
                  use the syntax of a destructor call on a non-class type, but there really is no
                  underlying destructor, it's a special syntax of the . and -> operators.
                  [color=blue]
                  > Note to other posters - nothing in the standard requires
                  > they 'don't do anything' - it would be permissible for int's
                  > destructor to set the bytes it occupied to '0xDEADBEEF' or
                  > '0xCDCDCDCD' -[/color]

                  int does NOT have a destructor.


                  Comment

                  • Andrey Tarasevich

                    #39
                    Re: destructor and delete

                    llewelly wrote:
                    [color=blue][color=green]
                    >> Since pointers are objects too, they will have a constructor and
                    >> destructor.[/color]
                    >
                    > built-in types have 'trivial destructors'. (That's what the standard
                    > calls them.)[/color]

                    No. Built-in types have no destructors at all. The standard never makes
                    any mention of built-in types having any destructors. Only class types
                    have destructors. Built-in types are not class types.

                    The definition of 'trivial destructor' is given in 12.4/3 and, as could
                    be expected, it is applicable to class types only.
                    [color=blue]
                    > Note to other posters - nothing in the standard requires
                    > they 'don't do anything' - it would be permissible for int's
                    > destructor to set the bytes it occupied to '0xDEADBEEF' or
                    > '0xCDCDCDCD' - and for a debugging implementation of C++, that
                    > would even be desirable.[/color]

                    The bytes can be set to anything, you are right. But it has nothing to
                    do with any destructors. Type 'int' is not a class type and, therefore,
                    it has no destructor at all.
                    [color=blue]
                    > The standard does define 'trivial
                    > destructors' in such a way as to *allow* a trivial destructor to
                    > do nothing, but there's no requirement.[/color]

                    That's true. But, once again, the notion of 'destruction' is not
                    applicable to non-class types.

                    --
                    Best regards,
                    Andrey Tarasevich
                    Brainbench C and C++ Programming MVP

                    Comment

                    • Default User

                      #40
                      Re: destructor and delete

                      jeffc wrote:
                      [color=blue]
                      > Pointers are not objects.[/color]


                      Sure they are. They just aren't objects of the type they point to.




                      Brian Rodenborn

                      Comment

                      • jeffc

                        #41
                        Re: destructor and delete


                        "Default User" <first.last@com pany.com> wrote in message
                        news:3F54FC66.5 082BA3F@company .com...[color=blue]
                        > jeffc wrote:
                        >[color=green]
                        > > Pointers are not objects.[/color]
                        >
                        > Sure they are. They just aren't objects of the type they point to.[/color]

                        No, not any more than ints are objects. In the context of this discussion,
                        that ain't happenin'. No one mentioned "smart pointers" or anything fancy,
                        either.


                        Comment

                        • Andrey Tarasevich

                          #42
                          Re: destructor and delete

                          jeffc wrote:[color=blue][color=green]
                          >>[color=darkred]
                          >> > Pointers are not objects.[/color]
                          >>
                          >> Sure they are. They just aren't objects of the type they point to.[/color]
                          >
                          > No, not any more than ints are objects. In the context of this discussion,
                          > that ain't happenin'. No one mentioned "smart pointers" or anything fancy,
                          > either.[/color]

                          It looks like your idea of an "object" is significantly different from
                          the standard meaning of the term "object" in C++. Ints are objects.
                          Pointers are objects too.

                          --
                          Best regards,
                          Andrey Tarasevich
                          Brainbench C and C++ Programming MVP

                          Comment

                          • Karl Heinz Buchegger

                            #43
                            Re: destructor and delete



                            Andrey Tarasevich wrote:[color=blue]
                            >
                            > jeffc wrote:[color=green][color=darkred]
                            > >>
                            > >> > Pointers are not objects.
                            > >>
                            > >> Sure they are. They just aren't objects of the type they point to.[/color]
                            > >
                            > > No, not any more than ints are objects. In the context of this discussion,
                            > > that ain't happenin'. No one mentioned "smart pointers" or anything fancy,
                            > > either.[/color]
                            >
                            > It looks like your idea of an "object" is significantly different from
                            > the standard meaning of the term "object" in C++. Ints are objects.
                            > Pointers are objects too.[/color]

                            But you agree that they are radically different to class objects?

                            --
                            Karl Heinz Buchegger
                            kbuchegg@gascad .at

                            Comment

                            • Ron Natalie

                              #44
                              Re: destructor and delete


                              "jeffc" <nobody@nowhere .com> wrote in message news:3f550ce0_1 @news1.prserv.n et...[color=blue]
                              >
                              > "Default User" <first.last@com pany.com> wrote in message
                              > news:3F54FC66.5 082BA3F@company .com...[color=green]
                              > > jeffc wrote:
                              > >[color=darkred]
                              > > > Pointers are not objects.[/color]
                              > >
                              > > Sure they are. They just aren't objects of the type they point to.[/color]
                              >
                              > No, not any more than ints are objects. In the context of this discussion,[/color]

                              The context of the discussion is C++. int's are objects, so are pointers.


                              Comment

                              • jeffc

                                #45
                                Re: destructor and delete


                                "Ron Natalie" <ron@sensor.com > wrote in message
                                news:3f55ef08$1 $23210$9a6e19ea @news.newshosti ng.com...[color=blue]
                                >
                                > "jeffc" <nobody@nowhere .com> wrote in message[/color]
                                news:3f550ce0_1 @news1.prserv.n et...[color=blue][color=green]
                                > >
                                > > "Default User" <first.last@com pany.com> wrote in message
                                > > news:3F54FC66.5 082BA3F@company .com...[color=darkred]
                                > > > jeffc wrote:
                                > > >
                                > > > > Pointers are not objects.
                                > > >
                                > > > Sure they are. They just aren't objects of the type they point to.[/color]
                                > >
                                > > No, not any more than ints are objects. In the context of this[/color][/color]
                                discussion,[color=blue]
                                >
                                > The context of the discussion is C++. int's are objects, so are[/color]
                                pointers.

                                Not good enough. There is a clear distinction between a built in type and
                                an instance of a class.


                                Comment

                                Working...