Can anybody help me about the memory leak

G

Ge Chunyuan

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
 
N

Neelesh Bodas

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
 
J

jg

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
 
N

Neelesh Bodas

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.

-N
 
1

1983ddd

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

1983ddd

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.
It cannot, simply because there is a missing semicolon after
definition of Point3D. Otherwise it does.

You are so funny.

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

Alf P. Steinbach

* (e-mail address 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.
 
1

1983ddd

* (e-mail address 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.
 
G

Ge Chunyuan

* (e-mail address 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?
 
N

Neelesh Bodas

* (e-mail address removed):
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>
 
A

Alf P. Steinbach

* Ge Chunyuan:
* (e-mail address 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?

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
 
J

James Kanze

* 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.)
 
P

Pete Becker

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top