473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Can anybody help me about the memory leak

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

Jul 24 '07 #1
7 1232
On Jul 24, 9:16 am, Ge Chunyuan <hhy...@gmail.comwrote:
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

Jul 24 '07 #2
jg
On Jul 23, 9:16 pm, Ge Chunyuan <hhy...@gmail.comwrote:
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

Jul 24 '07 #3
On Jul 24, 10:00 am, jg <jgu...@gmail.comwrote:
On Jul 23, 9:16 pm, Ge Chunyuan <hhy...@gmail.comwrote:
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

Jul 24 '07 #4
On Jul 24, 1:53 pm, Neelesh Bodas <neelesh.bo...@gmail.comwrote:
On Jul 24, 10:00 am, jg <jgu...@gmail.comwrote:
On Jul 23, 9:16 pm, Ge Chunyuan <hhy...@gmail.comwrote:
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 '07 #5
On Jul 24, 2:09 pm, "1983...@gmail.com" <1983...@gmail.comwrote:
On Jul 24, 1:53 pm, Neelesh Bodas <neelesh.bo...@gmail.comwrote:
On Jul 24, 10:00 am, jg <jgu...@gmail.comwrote:
On Jul 23, 9:16 pm, Ge Chunyuan <hhy...@gmail.comwrote:
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 '07 #6
On Jul 24, 10:16 am, "Alf P. Steinbach" <al...@start.nowrote:
* 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:ja*********@gmail.com
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

Jul 24 '07 #7
On 2007-07-24 09:18:26 -0400, James Kanze <ja*********@gmail.comsaid:
On Jul 24, 10:16 am, "Alf P. Steinbach" <al...@start.nowrote:
>* 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)

Jul 25 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? ...
17
by: José Joye | last post by:
Hi, I have implemented a Service that is responsible for getting messages from a MS MQ located on a remote machine. I'm getting memory leak from time to time (???). In some situation, it is...
4
by: Don Nell | last post by:
Hello Why is there a memory leak when this code is executed. for(;;) { ManagementScope scope = new ManagementScope(); scope.Options.Username="username"; scope.Options.Password="password";...
20
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
23
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; namespace MemoryLeak
8
by: Adrian | last post by:
Hi I have a JS program that runs localy (under IE6 only) on a PC but it has a memory leak (probably the known MS one!) What applications are there that I could use to look at the memory usage of...
7
by: Salvador | last post by:
Hi, I am using WMI to gather information about different computers (using win2K and win 2K3), checking common classes and also WMI load balance. My application runs every 1 minute and reports...
3
by: Jim Land | last post by:
Jack Slocum claims here http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript- memory-leaks/ that "almost every site you visit that uses JavaScript is leaking memory". ...
22
by: Peter | last post by:
I am using VS2008. I have a Windows Service application which creates Crystal Reports. This is a multi theaded application which can run several reports at one time. My problem - there is a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.