delete

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

    #1

    delete

    If I call delete on a pointer is it guarenteed to set that pointer to
    NULL on all implementations ?

    I've also seen "delete this" in a reference counting pointer
    implementation and am wondering what the effects of that are.

  • Victor Bazarov

    #2
    Re: delete

    Christopher wrote:
    If I call delete on a pointer is it guarenteed to set that pointer to
    NULL on all implementations ?
    Of course not. It's not supposed to do that. I know of no compiler
    that would actually do that.
    I've also seen "delete this" in a reference counting pointer
    implementation and am wondering what the effects of that are.
    The effects of 'delete' is calling the destructor and freeing the
    memory (usually).

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Joe Greer

      #3
      Re: delete

      Christopher <cpisz@austin.r r.comwrote in news:1194372582 .668032.83740
      @y27g2000pre.go oglegroups.com:
      If I call delete on a pointer is it guarenteed to set that pointer to
      NULL on all implementations ?
      Pretty much, its guaranteed not to set the pointer to NULL.
      >
      I've also seen "delete this" in a reference counting pointer
      implementation and am wondering what the effects of that are.
      >
      Yes, delete calls the destructor on the object and returns the memory to
      the available memory pool. It doesn't have any magic way to find all the
      pointers to the released memory and NULL them though.

      joe

      Comment

      • Scott McPhillips [MVP]

        #4
        Re: delete

        "Christophe r" <cpisz@austin.r r.comwrote in message
        news:1194372582 .668032.83740@y 27g2000pre.goog legroups.com...
        If I call delete on a pointer is it guarenteed to set that pointer to
        NULL on all implementations ?

        Nope. But it would be a real good idea to follow your delete by setting
        that pointer to NULL yourself. This will help you catch (mis)use of a
        deleted pointer during debugging.

        --
        Scott McPhillips [VC++ MVP]

        Comment

        • Pete Becker

          #5
          Re: delete

          On 2007-11-06 13:39:54 -0500, "Scott McPhillips [MVP]"
          <org-dot-mvps-at-scottmcpsaid:
          "Christophe r" <cpisz@austin.r r.comwrote in message
          news:1194372582 .668032.83740@y 27g2000pre.goog legroups.com...
          >If I call delete on a pointer is it guarenteed to set that pointer to
          >NULL on all implementations ?
          >
          >
          Nope. But it would be a real good idea to follow your delete by
          setting that pointer to NULL yourself. This will help you catch
          (mis)use of a deleted pointer during debugging.
          The benefits from doing this are mostly illusory.

          ~MyType()
          {
          delete my_member_ptr;
          my_member_ptr = 0; // silly
          }

          void delete_it(T *ptr)
          {
          delete ptr;
          ptr = 0; // silly, and doesn't affect the pointer that was used to call
          this function
          }

          That's not to say that setting pointers to NULL is never useful, but it
          should be done as part of an overall design, not as a hack that may or
          may not help during debugging.

          --
          Pete
          Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
          Standard C++ Library Extensions: a Tutorial and Reference
          (www.petebecker.com/tr1book)

          Comment

          • peter koch

            #6
            Re: delete

            On 6 Nov., 19:39, "Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp>
            wrote:
            "Christophe r" <cp...@austin.r r.comwrote in message
            >
            news:1194372582 .668032.83740@y 27g2000pre.goog legroups.com...
            >
            If I call delete on a pointer is it guarenteed to set that pointer to
            NULL on all implementations ?
            >
            Nope. But it would be a real good idea to follow your delete by setting
            that pointer to NULL yourself. This will help you catch (mis)use of a
            deleted pointer during debugging.
            >
            --
            Scott McPhillips [VC++ MVP]
            I don't think so. I could imagine setting the pointer to some
            (nonportable) value, that when accessed would generate a hardware
            exception could be a good idea when debugging, but setting it to a
            "legal" value should be done only in cases where it really is the
            right decision.

            /Peter

            Comment

            • ManicQin

              #7
              Re: delete

              The benefits from doing this are mostly illusory.
              >
              ~MyType()
              {
              delete my_member_ptr;
              my_member_ptr = 0; // silly
              >
              }
              >
              void delete_it(T *ptr)
              {
              delete ptr;
              ptr = 0; // silly, and doesn't affect the pointer that was used to call
              this function
              >
              }
              >
              maybe in the dTor it's silly
              but beside debug related issues, the code
              if (NULL == ptr)
              is quite common... IMHO it's always better to nullify the ptr after
              deleting him

              Comment

              • Ron Natalie

                #8
                Re: delete

                peter koch wrote:
                >>
                >>If I call delete on a pointer is it guarenteed to set that pointer to
                >>NULL on all implementations ?
                >Nope. But it would be a real good idea to follow your delete by setting
                >that pointer to NULL yourself. This will help you catch (mis)use of a
                >deleted pointer during debugging.
                >>
                >--
                >Scott McPhillips [VC++ MVP]
                I don't think so. I could imagine setting the pointer to some
                (nonportable) value, that when accessed would generate a hardware
                exception could be a good idea when debugging, but setting it to a
                "legal" value should be done only in cases where it really is the
                right decision.
                >
                >
                Nothing says that the pointer passed to delete is the only copy of
                that value in the program (the argument to delete isn't even necessarily
                an l-value).

                There are debug systems that do try to poison the memory to detect
                future references (they do this by usually by not reissuing the same
                memory). The C language folk envisioned the ability to also poison
                the pointer value (although I know of no system that does this) which
                is using an previously deleted pointer value is undefined behavior even
                if you don't dereference it.

                Comment

                • Pete Becker

                  #9
                  Re: delete

                  On 2007-11-07 07:14:42 -0500, ManicQin <ManicQin@gmail .comsaid:
                  >The benefits from doing this are mostly illusory.
                  >>
                  >~MyType()
                  >{
                  >delete my_member_ptr;
                  >my_member_pt r = 0; // silly
                  >>
                  >}
                  >>
                  >void delete_it(T *ptr)
                  >{
                  >delete ptr;
                  >ptr = 0; // silly, and doesn't affect the pointer that was used to call
                  >this function
                  >>
                  >}
                  >>
                  >
                  maybe in the dTor it's silly
                  but beside debug related issues, the code
                  if (NULL == ptr)
                  is quite common... IMHO it's always better to nullify the ptr after
                  deleting him
                  As I said in the part you snipped, "That's not to say that setting
                  pointers to NULL is never useful, but it should be done as part of an
                  overall design..."

                  --
                  Pete
                  Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
                  Standard C++ Library Extensions: a Tutorial and Reference
                  (www.petebecker.com/tr1book)

                  Comment

                  • terminator

                    #10
                    Re: delete

                    On Nov 6, 9:09 pm, Christopher <cp...@austin.r r.comwrote:
                    If I call delete on a pointer is it guarenteed to set that pointer to
                    NULL on all implementations ?
                    I thought that pointers are normally passed by value to 'delete',So I
                    doubt that any delete operator sets its operand to NULL.
                    I've also seen "delete this" in a reference counting pointer
                    implementation and am wondering what the effects of that are.
                    I do not code that way.

                    regards,
                    FM.

                    Comment

                    Working...