proper deletion of pointers

Discussion in 'C++' started by Dwight Army of Champions, Feb 21, 2010.

  1. Why are you supposed to set a pointer equal to zero after deleting it?
    At what times are you not supposed to do this? I've been using "delete
    x; x = 0;" all the time, so often, in fact, that I just put them both
    on the same line. If you are supposed to do this every time, then why
    bother having two separate commands?
    Dwight Army of Champions, Feb 21, 2010
    #1
    1. Advertising

  2. I see. But why would you ever use the pointer again? To point to
    another object? Why not just define and initialize another pointer,
    then?
    Dwight Army of Champions, Feb 21, 2010
    #2
    1. Advertising

  3. Dwight Army of Champions

    tonydee Guest

    On Feb 22, 5:40 am, Dwight Army of Champions
    <> wrote:
    > I see. But why would you ever use the pointer again? To point to
    > another object? Why not just define and initialize another pointer,
    > then?


    As an example, say you have a string class that allocated space for
    the text on the heap. When empty, the pointer can be 0. So, someone
    might do something like:

    my_string s;
    s = "abc";
    s.clear();
    s = "xyz";

    And you could implement this quite cleanly by newing the memory in
    operator=(), then deleting it in clear(). The destructor can just
    delete the pointer, not caring whether it's 0 or not, as deleting a 0
    pointer is a "no-operation".

    Hope that helps,
    Tony
    tonydee, Feb 22, 2010
    #3
  4. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Dwight Army of Champions wrote:

    > Why are you supposed to set a pointer equal to zero after deleting it?
    > At what times are you not supposed to do this? I've been using "delete
    > x; x = 0;" all the time, so often, in fact, that I just put them both
    > on the same line. If you are supposed to do this every time, then why
    > bother having two separate commands?


    The pointer deleted may not be writable:

    class foo_t {
    public:
    int *const ptr;
    foo_t() : ptr(new int) {
    }
    ~foo_t() {
    delete ptr; // ptr is read only
    }
    };
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.9 (GNU/Linux)

    iEYEARECAAYFAkuCoA0ACgkQm4klUUKw07AYoQCfY+XGNXTbwt6DmZtKQFjem2oY
    FcwAnjoggWcVldlaJ8M597rD4I4V9WfS
    =3O5X
    -----END PGP SIGNATURE-----
    Michael Tsang, Feb 22, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles Hixson
    Replies:
    0
    Views:
    47
    Charles Hixson
    Apr 25, 2014
  2. Chris Angelico
    Replies:
    0
    Views:
    56
    Chris Angelico
    Apr 25, 2014
  3. Matthew Barnett
    Replies:
    0
    Views:
    57
    Matthew Barnett
    Apr 25, 2014
  4. Terry Reedy
    Replies:
    0
    Views:
    58
    Terry Reedy
    Apr 25, 2014
  5. Tim Chase
    Replies:
    0
    Views:
    75
    Tim Chase
    Apr 25, 2014
Loading...

Share This Page