a deconstructor question

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

    #1

    a deconstructor question

    Dear All,

    I am a little confused.

    //Objects of this class are partially filled arrays of doubles
    class PFArray
    {
    public:
    ...
    ~PFArray();
    private:
    double *a; //for an array of doubles
    ..
    };

    PFArray::~PFArr ay()
    {
    delete [] a;
    }

    The code "double *a" in the private section of class PFArray doesn't
    NECESSARILY tell that a is a pointer to an array (it coulde be a pointer
    to a double, right?). Why the deconstructor use " delete [] a "? If a
    instead is pointer to a double, then "delete a" should be used?



  • Mike Wahler

    #2
    Re: a deconstructor question


    "Xiaoshen Li" <xli6@gmu.edu > wrote in message
    news:dp3k73$eq2 j$1@osf1.gmu.ed u...[color=blue]
    > Dear All,
    >
    > I am a little confused.
    >
    > //Objects of this class are partially filled arrays of doubles
    > class PFArray
    > {
    > public:
    > ...
    > ~PFArray();
    > private:
    > double *a; //for an array of doubles
    > ..
    > };
    >
    > PFArray::~PFArr ay()
    > {
    > delete [] a;
    > }
    >
    > The code "double *a" in the private section of class PFArray doesn't
    > NECESSARILY tell that a is a pointer to an array (it coulde be a pointer
    > to a double, right?).[/color]

    It *is* a pointer to type double. A pointer to an array would look like
    e.g:

    double (*a)[size];
    [color=blue]
    > Why the deconstructor use " delete [] a "?[/color]

    It should only use 'delete[]' if the value of the pointer
    'a' was returned 'new[]' or is NULL (0).
    Otherwise the behavior is undefined.
    [color=blue]
    > If a instead is pointer to a double, then "delete a" should be used?[/color]

    Only if 'a's value was returned by 'new' (not '[]').

    a = new double; // allocates a single type double object
    delete a; // deallocate the single type double object
    // delete[] a; // undefined behavior.

    a = new double[5]; // allocates array of five type double objects
    delete[] a; // deallocate the array of five doubles//
    delete a; // undefined behavior.

    double d;
    a = &d; // 'a's value not from 'new' or 'new[]'
    delete d; // undefined behavior
    delete[] d; // undefined behavior

    Which C++ book(s) are you reading?

    -Mike


    Comment

    • Xiaoshen Li

      #3
      Re: a deconstructor question

      Thank you for the help. But I am not sure you are right.
      I took C++ class three years ago. Since then, I didn't use it.
      Yesterday, I saw the code in the class passed by the professor:

      class Person
      {
      public:
      ..
      ~Person();
      private:
      char *name;
      };

      Person::~Person ()
      {
      delete [] name;
      }

      Since name could be pointing a single char or a char array, why the
      destructor uses "delete [] name"?

      Thank you again.

      Comment

      • Jacek Dziedzic

        #4
        Re: a deconstructor question

        Xiaoshen Li wrote:[color=blue]
        > Thank you for the help. But I am not sure you are right.[/color]

        He is.
        [color=blue]
        > I took C++ class three years ago. Since then, I didn't use it.
        > Yesterday, I saw the code in the class passed by the professor:
        >
        > class Person
        > {
        > public:
        > ..
        > ~Person();
        > private:
        > char *name;
        > };
        >
        > Person::~Person ()
        > {
        > delete [] name;
        > }
        >
        > Since name could be pointing a single char or a char array, why the
        > destructor uses "delete [] name"?[/color]

        The rule is -- if you allocate with new[], you deallocate
        with delete[]. If you allocate with new, you deallocate with
        delete.

        Therefore the code by "the professor" is fine only if storage
        for 'name' is allocated with new[] (which is most probably
        the case).

        HTH,
        - J.

        Comment

        • Mike Wahler

          #5
          Re: a deconstructor question


          "Xiaoshen Li" <xli6@gmu.edu > wrote in message
          news:dp3ni9$17t 5$1@osf1.gmu.ed u...[color=blue]
          > Thank you for the help. But I am not sure you are right.[/color]

          Why not? What specifically did I state that you
          believe is not correct.
          [color=blue]
          > I took C++ class three years ago. Since then, I didn't use it. Yesterday,
          > I saw the code in the class passed by the professor:
          >
          > class Person
          > {
          > public:
          > ..[/color]

          What's the code you left out?
          [color=blue]
          > ~Person();
          > private:
          > char *name;
          > };
          >
          > Person::~Person ()
          > {
          > delete [] name;
          > }
          >
          > Since name could be pointing a single char or a char array, why the
          > destructor uses "delete [] name"?[/color]

          Because it's assuming that 'name' was given a value by
          'new[]' (not 'new', nor the address operator). If this
          assumption proves false, the code is broken.

          Anyway, you should not be using 'C-style' strings,
          use a std::string object instead, then all the issues of
          allocation/deallocation disappear (the std:: string objects
          handle their own memory management for you automatically).

          #include <string>

          class Person
          {
          std::string name;
          }; // no need for 'new' or 'delete'

          -Mike


          Comment

          • Xiaoshen Li

            #6
            Re: a deconstructor question

            Thank you very much, Mike.

            #include <string>
            using namespace std;

            class Person
            {
            publice:
            ...
            private:
            string name;
            int age;
            char gender;
            }

            With above class, is destructor needed or not? My guess is not, am I
            correct?

            Mike Wahler wrote:
            [color=blue]
            >
            > Anyway, you should not be using 'C-style' strings,
            > use a std::string object instead, then all the issues of
            > allocation/deallocation disappear (the std:: string objects
            > handle their own memory management for you automatically).
            >
            > #include <string>
            >
            > class Person
            > {
            > std::string name;
            > }; // no need for 'new' or 'delete'
            >
            > -Mike
            >
            >[/color]

            Comment

            • Paul Henderson

              #7
              Re: a deconstructor question

              Nope, no destructor needed there, as name will delete its own buffer.
              But don't import std...use std::string!

              Comment

              • Xiaoshen Li

                #8
                Re: a deconstructor question

                Could you kindly elaborate why "don't import std, instead use
                std::string"? Thank you very much.

                Paul Henderson wrote:[color=blue]
                > Nope, no destructor needed there, as name will delete its own buffer.
                > But don't import std...use std::string!
                >[/color]

                Comment

                • Paul Henderson

                  #9
                  Re: a deconstructor question

                  Oh, just better practice in many people's opinion [though I might start
                  an argument if I say that too strongly]. Basically, there's no point in
                  *having* the std namespace if you're just going to include it at global
                  scope everywhere, and there's a danger of symbol-name conflicts between
                  your code and bits of std. But it doesn't really matter :-)

                  Comment

                  • Gavin Deane

                    #10
                    Re: a deconstructor question


                    Paul Henderson wrote:

                    <suggesting avoiding using namspace std>
                    [color=blue]
                    > Oh, just better practice in many people's opinion [though I might start
                    > an argument if I say that too strongly]. Basically, there's no point in
                    > *having* the std namespace if you're just going to include it at global
                    > scope everywhere, and there's a danger of symbol-name conflicts between
                    > your code and bits of std. But it doesn't really matter :-)[/color]

                    Please quote some context in your message.

                    It is true that if you put a using directive or using declaration in
                    your own source file, it only affects you. As long as you understand
                    the issue, it's up to you whether you do it. However, the code that
                    sparked this discussion was a class definition, which could well reside
                    in a header file. If so, the argument for explicitly qualifying names
                    from the std namespace in preference to a using directive or
                    declaration should be made much more strongly.

                    If you put using namespace std or even using std::string in a header
                    file, *everyone* who includes your header file gets the namespace
                    pollution, whether they want it or not. This is very different from you
                    deciding to accept namespace pollution contained within your own source
                    file.

                    So it might be fair to say "But it doesn't really matter" if you are
                    talking about a source file, particularly in a toy program you are
                    writing for practice. But in a header file it really does matter. Don't
                    put using directives or using declarations in header files.

                    Gavin Deane

                    Comment

                    • Mateusz Łoskot

                      #11
                      Re: a deconstructor question

                      Xiaoshen Li wrote:[color=blue]
                      > Dear All,
                      >
                      > I am a little confused.
                      >
                      > //Objects of this class are partially filled arrays of doubles
                      > class PFArray
                      > {
                      > public:
                      > ...
                      > ~PFArray();
                      > private:
                      > double *a; //for an array of doubles
                      > ..
                      > };
                      >
                      > PFArray::~PFArr ay()
                      > {
                      > delete [] a;
                      > }[/color]


                      Put raw arrays away.
                      Use std::vector for elements of type of double.

                      Cheers
                      --
                      Mateusz Łoskot

                      Comment

                      • Mateusz Łoskot

                        #12
                        Re: a deconstructor question

                        Xiaoshen Li wrote:[color=blue]
                        > Thank you for the help. But I am not sure you are right.
                        > I took C++ class three years ago. Since then, I didn't use it.[/color]

                        Do you mean you didn't use delete[] ?
                        Look here:
                        http://www.softsurfer.com/Archive/al...poly_simplify()

                        Point* vt = new Point[n]; // vertex buffer
                        int* mk = new int[n] = {0}; // marker buffer
                        // ...
                        delete vt;
                        delete mk;

                        There are still big amount of old code with this buggy way of destroying
                        arrays: delete instead of delete[]
                        [color=blue]
                        > Since name could be pointing a single char or a char array, why the
                        > destructor uses "delete [] name"?[/color]



                        Cheers
                        --
                        Mateusz Łoskot

                        Comment

                        Working...