Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Can anybody help me about the memory leak

Reply
Thread Tools

Can anybody help me about the memory leak

 
 
Ge Chunyuan
Guest
Posts: n/a
 
      07-24-2007
Eg. we have the class hiaerarchy Point inherit Point3D

class Point
{
public:
Point(int a=0,char* b=0)(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

 
Reply With Quote
 
 
 
 
Neelesh Bodas
Guest
Posts: n/a
 
      07-24-2007
On Jul 24, 9:16 am, Ge Chunyuan <(E-Mail Removed)> wrote:
> Eg. we have the class hiaerarchy Point inherit Point3D
>
> class Point
> {
> public:
> Point(int a=0,char* b=0)(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

 
Reply With Quote
 
 
 
 
jg
Guest
Posts: n/a
 
      07-24-2007
On Jul 23, 9:16 pm, Ge Chunyuan <(E-Mail Removed)> wrote:
> Eg. we have the class hiaerarchy Point inherit Point3D
>
> class Point
> {
> public:
> Point(int a=0,char* b=0)(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:oint3D()
as well as Point:oint() must be defined.

Can your code be compiled successfully ?

JG

 
Reply With Quote
 
Neelesh Bodas
Guest
Posts: n/a
 
      07-24-2007
On Jul 24, 10:00 am, jg <(E-Mail Removed)> wrote:
> On Jul 23, 9:16 pm, Ge Chunyuan <(E-Mail Removed)> wrote:
>
>
>
> > Eg. we have the class hiaerarchy Point inherit Point3D

>
> > class Point
> > {
> > public:
> > Point(int a=0,char* b=0)(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:oint3D()
> as well as Point:oint() must be defined.
>


Point:oint(int =0, char* = 0) can act as Point:oint() 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

 
Reply With Quote
 
1983ddd@gmail.com
Guest
Posts: n/a
 
      07-24-2007
On Jul 24, 1:53 pm, Neelesh Bodas <(E-Mail Removed)> wrote:
> On Jul 24, 10:00 am, jg <(E-Mail Removed)> wrote:
>
>
>
> > On Jul 23, 9:16 pm, Ge Chunyuan <(E-Mail Removed)> wrote:

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

>
> > > class Point
> > > {
> > > public:
> > > Point(int a=0,char* b=0)(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:oint3D()
> > as well as Point:oint() must be defined.

>
> Point:oint(int =0, char* = 0) can act as Point:oint() 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



 
Reply With Quote
 
1983ddd@gmail.com
Guest
Posts: n/a
 
      07-24-2007
On Jul 24, 2:09 pm, "(E-Mail Removed)" <(E-Mail Removed)> wrote:
> On Jul 24, 1:53 pm, Neelesh Bodas <(E-Mail Removed)> wrote:
>
>
>
> > On Jul 24, 10:00 am, jg <(E-Mail Removed)> wrote:

>
> > > On Jul 23, 9:16 pm, Ge Chunyuan <(E-Mail Removed)> wrote:

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

>
> > > > class Point
> > > > {
> > > > public:
> > > > Point(int a=0,char* b=0)(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:oint3D()
> > > as well as Point:oint() must be defined.

>
> > Point:oint(int =0, char* = 0) can act as Point:oint() 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.

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-24-2007
* http://www.velocityreviews.com/forums/(E-Mail Removed):
>
> 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?
 
Reply With Quote
 
1983ddd@gmail.com
Guest
Posts: n/a
 
      07-24-2007
On Jul 24, 2:26 pm, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> * (E-Mail Removed):
>
>
>
> > 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.

 
Reply With Quote
 
Ge Chunyuan
Guest
Posts: n/a
 
      07-24-2007
On Jul 24, 2:26 pm, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> * (E-Mail Removed):
>
>
>
> > 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?

 
Reply With Quote
 
Neelesh Bodas
Guest
Posts: n/a
 
      07-24-2007
On Jul 24, 12:57 pm, Ge Chunyuan <(E-Mail Removed)> wrote:
> On Jul 24, 2:26 pm, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
>
>
>
> > * (E-Mail Removed):

>
> > > 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-lit....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>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory leak even after deleting memory pointers from vector cham C++ 5 09-25-2008 10:30 AM
Leak or no leak ?? Richard Heathfield C Programming 4 07-10-2006 11:37 AM
Dynamic memory allocation and memory leak... s.subbarayan C Programming 10 03-22-2005 02:48 PM
Memory leak??? (top reporting high memory usage under Solaris) Mark Probert Ruby 4 02-09-2005 06:13 PM
Can you help me find my memory leak? Greg Baker C++ 4 09-02-2003 08:38 AM



Advertisments