Can anybody help me about the memory leak

Discussion in 'C++' started by Ge Chunyuan, Jul 24, 2007.

  1. Ge Chunyuan

    Ge Chunyuan Guest

    Eg. we have the class hiaerarchy Point inherit Point3D

    class Point
    {
    public:
    Point(int a=0,char* b=0):x(a),y(b){}
    private:
    int x;
    char* y;
    };

    class Point3D: public Point
    {
    public:
    Point3D(char*c=0):z(c){}
    private:
    char* z;
    }

    Once we instatiate an array like this:
    Point* pPointArray = new Point3D[10];
    delete[] pPointArray;

    why this will cause memory leak?
    will delete recursively invoke Point3D's destructor.



    Thanks indeed
    Ge Chunyuan
    Ge Chunyuan, Jul 24, 2007
    #1
    1. Advertising

  2. On Jul 24, 9:16 am, Ge Chunyuan <> wrote:
    > Eg. we have the class hiaerarchy Point inherit Point3D
    >
    > class Point
    > {
    > public:
    > Point(int a=0,char* b=0):x(a),y(b){}
    > private:
    > int x;
    > char* y;
    >
    > };
    >
    > class Point3D: public Point
    > {
    > public:
    > Point3D(char*c=0):z(c){}
    > private:
    > char* z;
    >
    > }


    missing semicolon

    >
    > Once we instatiate an array like this:
    > Point* pPointArray = new Point3D[10];
    > delete[] pPointArray;
    >
    > why this will cause memory leak?
    > will delete recursively invoke Point3D's destructor.
    >


    1. The base class destructor must be virtual if you wish to delete a
    derived class object through the base class pointer.

    2. Deleting array of derived class objects through base class pointer
    is undefined.

    3. 'Never Treat Arrays Polymporphically' : More Effective C++ (Point
    3).

    -N
    Neelesh Bodas, Jul 24, 2007
    #2
    1. Advertising

  3. Ge Chunyuan

    jg Guest

    On Jul 23, 9:16 pm, Ge Chunyuan <> wrote:
    > Eg. we have the class hiaerarchy Point inherit Point3D
    >
    > class Point
    > {
    > public:
    > Point(int a=0,char* b=0):x(a),y(b){}
    > private:
    > int x;
    > char* y;
    >
    > };
    >
    > class Point3D: public Point
    > {
    > public:
    > Point3D(char*c=0):z(c){}
    > private:
    > char* z;
    >
    > }
    >
    > Once we instatiate an array like this:
    > Point* pPointArray = new Point3D[10];
    > delete[] pPointArray;
    >
    > why this will cause memory leak?
    > will delete recursively invoke Point3D's destructor.
    >
    > Thanks indeed
    > Ge Chunyuan


    To declare an array of Point3D, the default ctor Point3D::point3D()
    as well as Point::point() must be defined.

    Can your code be compiled successfully ?

    JG
    jg, Jul 24, 2007
    #3
  4. On Jul 24, 10:00 am, jg <> wrote:
    > On Jul 23, 9:16 pm, Ge Chunyuan <> wrote:
    >
    >
    >
    > > Eg. we have the class hiaerarchy Point inherit Point3D

    >
    > > class Point
    > > {
    > > public:
    > > Point(int a=0,char* b=0):x(a),y(b){}
    > > private:
    > > int x;
    > > char* y;

    >
    > > };

    >
    > > class Point3D: public Point
    > > {
    > > public:
    > > Point3D(char*c=0):z(c){}
    > > private:
    > > char* z;

    >
    > > }

    >
    > > Once we instatiate an array like this:
    > > Point* pPointArray = new Point3D[10];
    > > delete[] pPointArray;

    >
    > > why this will cause memory leak?
    > > will delete recursively invoke Point3D's destructor.

    >
    > > Thanks indeed
    > > Ge Chunyuan

    >
    > To declare an array of Point3D, the default ctor Point3D::point3D()
    > as well as Point::point() must be defined.
    >


    Point::point(int =0, char* = 0) can act as Point::point() since all
    its arguments have default values.


    > Can your code be compiled successfully ?
    >


    It cannot, simply because there is a missing semicolon after
    definition of Point3D. Otherwise it does.

    > JG


    -N
    Neelesh Bodas, Jul 24, 2007
    #4
  5. Ge Chunyuan

    Guest

    On Jul 24, 1:53 pm, Neelesh Bodas <> wrote:
    > On Jul 24, 10:00 am, jg <> wrote:
    >
    >
    >
    > > On Jul 23, 9:16 pm, Ge Chunyuan <> wrote:

    >
    > > > Eg. we have the class hiaerarchy Point inherit Point3D

    >
    > > > class Point
    > > > {
    > > > public:
    > > > Point(int a=0,char* b=0):x(a),y(b){}
    > > > private:
    > > > int x;
    > > > char* y;

    >
    > > > };

    >
    > > > class Point3D: public Point
    > > > {
    > > > public:
    > > > Point3D(char*c=0):z(c){}
    > > > private:
    > > > char* z;

    >
    > > > }

    >
    > > > Once we instatiate an array like this:
    > > > Point* pPointArray = new Point3D[10];
    > > > delete[] pPointArray;

    >
    > > > why this will cause memory leak?
    > > > will delete recursively invoke Point3D's destructor.

    >
    > > > Thanks indeed
    > > > Ge Chunyuan

    >
    > > To declare an array of Point3D, the default ctor Point3D::point3D()
    > > as well as Point::point() must be defined.

    >
    > Point::point(int =0, char* = 0) can act as Point::point() since all
    > its arguments have default values.
    >
    > > Can your code be compiled successfully ?

    >
    > It cannot, simply because there is a missing semicolon after
    > definition of Point3D. Otherwise it does.


    You are so funny.

    > > JG

    >
    > -N
    , Jul 24, 2007
    #5
  6. Ge Chunyuan

    Guest

    On Jul 24, 2:09 pm, "" <> wrote:
    > On Jul 24, 1:53 pm, Neelesh Bodas <> wrote:
    >
    >
    >
    > > On Jul 24, 10:00 am, jg <> wrote:

    >
    > > > On Jul 23, 9:16 pm, Ge Chunyuan <> wrote:

    >
    > > > > Eg. we have the class hiaerarchy Point inherit Point3D

    >
    > > > > class Point
    > > > > {
    > > > > public:
    > > > > Point(int a=0,char* b=0):x(a),y(b){}
    > > > > private:
    > > > > int x;
    > > > > char* y;

    >
    > > > > };

    >
    > > > > class Point3D: public Point
    > > > > {
    > > > > public:
    > > > > Point3D(char*c=0):z(c){}
    > > > > private:
    > > > > char* z;

    >
    > > > > }

    >
    > > > > Once we instatiate an array like this:
    > > > > Point* pPointArray = new Point3D[10];
    > > > > delete[] pPointArray;

    >
    > > > > why this will cause memory leak?
    > > > > will delete recursively invoke Point3D's destructor.

    >
    > > > > Thanks indeed
    > > > > Ge Chunyuan

    >
    > > > To declare an array of Point3D, the default ctor Point3D::point3D()
    > > > as well as Point::point() must be defined.

    >
    > > Point::point(int =0, char* = 0) can act as Point::point() since all
    > > its arguments have default values.

    >
    > > > Can your code be compiled successfully ?

    >
    > > It cannot, simply because there is a missing semicolon after
    > > definition of Point3D. Otherwise it does.

    >
    > You are so funny.
    >
    > > > JG

    >
    > > -N

    >
    >


    But I want to know where it will leak? I have no found any new
    operation except Point = new ...
    Forgive me my shallow about memory leak. Thank you.
    , Jul 24, 2007
    #6
  7. * :
    >
    > But I want to know where it will leak? I have no found any new
    > operation except Point = new ...
    > Forgive me my shallow about memory leak. Thank you.


    The code may or may not leak, because it exhibits Undefined Behavior.
    It exhibits UB because it calls delete[] with a pointer of different
    static type than the type used in the new[] expression. Ref. §5.3.5/3.

    --
    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?
    Alf P. Steinbach, Jul 24, 2007
    #7
  8. Ge Chunyuan

    Guest

    On Jul 24, 2:26 pm, "Alf P. Steinbach" <> wrote:
    > * :
    >
    >
    >
    > > But I want to know where it will leak? I have no found any new
    > > operation except Point = new ...
    > > Forgive me my shallow about memory leak. Thank you.

    >
    > The code may or may not leak, because it exhibits Undefined Behavior.
    > It exhibits UB because it calls delete[] with a pointer of different
    > static type than the type used in the new[] expression. Ref. §5.3.5/3.
    >
    > --
    > 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?


    Thanks again.
    , Jul 24, 2007
    #8
  9. Ge Chunyuan

    Ge Chunyuan Guest

    On Jul 24, 2:26 pm, "Alf P. Steinbach" <> wrote:
    > * :
    >
    >
    >
    > > But I want to know where it will leak? I have no found any new
    > > operation except Point = new ...
    > > Forgive me my shallow about memory leak. Thank you.

    >
    > The code may or may not leak, because it exhibits Undefined Behavior.
    > It exhibits UB because it calls delete[] with a pointer of different
    > static type than the type used in the new[] expression. Ref. §5.3.5/3.
    >
    > --
    > 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?


    thanks so much,
    but where can I access the so-called Ref. 5.3.5/3?
    Ge Chunyuan, Jul 24, 2007
    #9
  10. On Jul 24, 12:57 pm, Ge Chunyuan <> wrote:
    > On Jul 24, 2:26 pm, "Alf P. Steinbach" <> wrote:
    >
    >
    >
    > > * :

    >
    > > > But I want to know where it will leak? I have no found any new
    > > > operation except Point = new ...
    > > > Forgive me my shallow about memory leak. Thank you.

    >
    > > The code may or may not leak, because it exhibits Undefined Behavior.
    > > It exhibits UB because it calls delete[] with a pointer of different
    > > static type than the type used in the new[] expression. Ref. §5.3.5/3.

    >
    > > --
    > > 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?

    >
    > thanks so much,
    > but where can I access the so-called Ref. 5.3.5/3?


    He is talking about the reference from the C++ ISO standard. You can
    know more about this here:
    http://www.parashift.com/c -faq-lite/big-picture.html#faq-6.13

    As an aside, FYI: 5.3.5/3 says this:

    <quote>
    5.3.5/3: In the first alternative (delete object ), if the static type
    of the operand is different from its dynamic type, the static type
    shall be a base class of the operand's dynamic type and the static
    type shall have a virtual destructor or the behavior is undefined. In
    the second alternative (delete array ) if the dynamic type of the
    object to be deleted differs from its static type, the behavior is
    undefined
    </quote>
    Neelesh Bodas, Jul 24, 2007
    #10
  11. * Ge Chunyuan:
    > On Jul 24, 2:26 pm, "Alf P. Steinbach" <> wrote:
    >> * :
    >>
    >>
    >>
    >>> But I want to know where it will leak? I have no found any new
    >>> operation except Point = new ...
    >>> Forgive me my shallow about memory leak. Thank you.

    >> The code may or may not leak, because it exhibits Undefined Behavior.
    >> It exhibits UB because it calls delete[] with a pointer of different
    >> static type than the type used in the new[] expression. Ref. §5.3.5/3.
    >>
    >> --
    >> 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?

    >
    > thanks so much,
    > but where can I access the so-called Ref. 5.3.5/3?


    You can buy the ISO C++ standard (the latest revision is the 2003
    version, the "TC1", Technical Corrigendum 1) from ISO, in PDF format.

    Or you can get almost the real thing by using a draft version, which is
    free but necessarily differs in some small details from the official
    standard.

    Bjarne Stroustrup's FAQ links to the last pre-standard draft (which
    omits some things in the final standard, and does not incorporate the
    corrections of C++ 2003), and I believe the C++ committee pages link to
    the latest draft (which has many things not yet officially standard).

    Now, in the future please don't quote blank lines, nor signatures.

    See the C++ FAQ Lite about how to quote etc.; it also provides answers
    to a great many frequently asked questions (hence, "FAQ").

    Cheers,

    - Alf

    --
    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?
    Alf P. Steinbach, Jul 24, 2007
    #11
  12. Ge Chunyuan

    James Kanze Guest

    On Jul 24, 10:16 am, "Alf P. Steinbach" <> wrote:
    > * Ge Chunyuan:
    > and I believe the C++ committee pages link to
    > the latest draft (which has many things not yet officially standard).


    I'm not sure off hand if you can access this without being a
    member or not. But if you can, it's probably the better
    solution. For the most, the changes are marked, so you can
    still see what the official standard said. (This is not
    necessarily true when the change is the introduction of an
    entire new section, and there's no indication, for example, that
    §23.4 doesn't exist at all in the currently official version.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jul 24, 2007
    #12
  13. Ge Chunyuan

    Pete Becker Guest

    On 2007-07-24 09:18:26 -0400, James Kanze <> said:

    > On Jul 24, 10:16 am, "Alf P. Steinbach" <> wrote:
    >> * Ge Chunyuan:
    >> and I believe the C++ committee pages link to
    >> the latest draft (which has many things not yet officially standard).

    >
    > I'm not sure off hand if you can access this without being a
    > member or not. But if you can, it's probably the better
    > solution. For the most, the changes are marked, so you can
    > still see what the official standard said. (This is not
    > necessarily true when the change is the introduction of an
    > entire new section, and there's no indication, for example, that
    > §23.4 doesn't exist at all in the currently official version.)


    The change bars show changes from the previous draft, not from the
    current standard. That includes new and removed sections, but, as with
    all the other changes, only for the first revision where the change
    occurred.

    --
    Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
    Standard C++ Library Extensions: a Tutorial and Reference
    (www.petebecker.com/tr1book)
    Pete Becker, Jul 25, 2007
    #13
    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. Greg Baker
    Replies:
    4
    Views:
    429
    Karl Heinz Buchegger
    Sep 2, 2003
  2. s.subbarayan

    Dynamic memory allocation and memory leak...

    s.subbarayan, Mar 18, 2005, in forum: C Programming
    Replies:
    10
    Views:
    699
    Eric Sosman
    Mar 22, 2005
  3. Richard Heathfield

    Leak or no leak ??

    Richard Heathfield, Jul 10, 2006, in forum: C Programming
    Replies:
    4
    Views:
    350
    Richard Heathfield
    Jul 10, 2006
  4. cham
    Replies:
    5
    Views:
    768
  5. Mark Probert
    Replies:
    4
    Views:
    326
    Mark Probert
    Feb 9, 2005
Loading...

Share This Page