473,385 Members | 2,274 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,385 software developers and data experts.

pointer to a class

Hi all, I have a question about the code below. Any idea abuot that,
thanks in advance!

myclass.h
class myclass
{
public:
myclass();
~myclass();
void setdata();

private:
yclass *ypr;
};

myclass.cpp
include"myclass.h"
include"yclass.h"

myclass::myclass()
{
yclass *ypr=new yclass;
}

myclass::~myclass()
{
delete ypr;
}

void myclass::setdata()
{
yclass *newpr=new yclass;
ypr=newpr; //My question is here. If I delete the "*ypr" pointer in
the destructor function of myclass, does the "*newpr" pointer being
deleted automatically since both of them have the same address? the
newpr class object has the same lifetime as myclass(after it was
created), and I have no idea when it should be deleted.
}

Jul 23 '05 #1
7 1557
an*****@hotmail.com wrote:
My question is here. If I delete the "*ypr" pointer in
the destructor function of myclass, does the "*newpr" pointer being
deleted automatically since both of them have the same address?
They don't have the same address. myclass::yptr points to one instance
of a yclass, and newpr points to another instance of yclass. General
rule of thumb: you need 1 call to 'delete' for every call to 'new'.
You have 2 calls to 'new', but only 1 call to 'delete'. Memory leak.
the newpr class object has the same lifetime as myclass(after it was
created), and I have no idea when it should be deleted.


Do you really need to instantiate the yclass outside of myclass' ctor?
Just let myclass manage it's own resources.

Take care,

John Dibling

Jul 23 '05 #2

<an*****@hotmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Hi all, I have a question about the code below. Any idea abuot that,
thanks in advance!

myclass.h
class myclass
{
public:
myclass();
~myclass();
void setdata();

private:
yclass *ypr;
};

myclass.cpp
include"myclass.h"
include"yclass.h"

myclass::myclass()
{
yclass *ypr=new yclass;
That should be just

ypr = new yclass;

As it is, you're declaring (and then leaking) new local variable, not
setting the value of the member.
}

myclass::~myclass()
{
delete ypr;
}

void myclass::setdata()
{
yclass *newpr=new yclass;
ypr=newpr; //My question is here. If I delete the "*ypr" pointer in
the destructor function of myclass, does the "*newpr" pointer being
deleted automatically since both of them have the same address? the
newpr class object has the same lifetime as myclass(after it was
created), and I have no idea when it should be deleted.
}


The variable newpr is not a "class object", it's a pointer. There is just
one object here, and it is deleted when *any* pointer to it is deleted. But
when this function ends, the local newpr pointer is not deleted, it goes out
of scope, and simply doesn't exist any more. The object it pointed to lives
on, and in this case is now pointed to by the ypr member pointer. (So, you
don't have to worry about calling delete for it.)

But...why are you using the temporary at all? Why not just "ypr = new
yclass;"?

However!!! Notice that whatever ypr pointed to before you ran this function
is now leaked (i.e., it never gets destroyed). If you want to change what
ypr points to, you need to first delete ypr, then assign it a new instance
of yclass.

A note on your style: generally, a function with a name like "setdata" would
be passed whatever data it is you want to "set". The name is somewhat
misleading, if what it's doing is creating a new yclass member.

Finally, read up on the "rule of three". Whenever you have a dynamic member
(a pointer) like this, it's likely that you'll need a copy constructor and
an assignment operator as well.

-Howard
Jul 23 '05 #3

"John Dibling" <jd******@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
an*****@hotmail.com wrote:
My question is here. If I delete the "*ypr" pointer in
the destructor function of myclass, does the "*newpr" pointer being
deleted automatically since both of them have the same address?
They don't have the same address. myclass::yptr points to one instance
of a yclass, and newpr points to another instance of yclass. General
rule of thumb: you need 1 call to 'delete' for every call to 'new'.
You have 2 calls to 'new', but only 1 call to 'delete'. Memory leak.


Well, they do point to the same address after this code:
yclass *newpr=new yclass;
ypr=newpr;


But, the problem is that ypr already pointed to something *before* this
code, and *that* object was leaked by simply assigning it a new value,
without deleting the existing object first.

(Just wanted to clarify where the leak occurred.)

-Howard

Jul 23 '05 #4
an*****@hotmail.com wrote:
Hi all, I have a question about the code below. Any idea abuot that,
thanks in advance!

myclass.h
class myclass
{
public:
myclass();
~myclass();
void setdata();

private:
yclass *ypr;
};

myclass.cpp
include"myclass.h"
include"yclass.h"
This shouldn't compile, because yclass in unknown in myclass.h.
myclass::myclass()
{
yclass *ypr=new yclass;
}
This defines a new local pointer in the constructor and initializes it with
the address of a new dynamically allocated object. Then, the constructor
finishes, the pointer goes out of scope, and the dynamic object becomes a
memory leak, because you lost the only pointer to it.
Probably, you rather want something like:

myclass::myclass()
: ypr(new yclass)
{
}
myclass::~myclass()
{
delete ypr;
}

void myclass::setdata()
{
yclass *newpr=new yclass;
ypr=newpr; //My question is here. If I delete the "*ypr" pointer in
the destructor function of myclass, does the "*newpr" pointer being
deleted automatically since both of them have the same address? the
newpr class object has the same lifetime as myclass(after it was
created), and I have no idea when it should be deleted.
}


You seem to be confusing the pointers with the objects they point to. If you
change the constructor like I wrote above, ypr already points to an object
of class yclass. So what happens in setdata is the following:
You create a second object of class yclass and initialize newpr with it.
Then you let ypr point to that same new object. Then the function ends and
newpr (only the pointer itself, nothing else) gets out of scope. So ypr now
points to the newly created object. When your myclass object gets
destroyed, its destructor will delete that one.
However, there is still the object that ypr has been pointing to before
setdata was called. Since you overwrote ypr with the address of another
object, the old object is lost. You don't have any pointer to it anymore,
so you can never delete it anymore.
So what you have to do is delete the old object within setdata:

void myclass::setdata()
{
yclass *newpr=new yclass;
delete ypr;
ypr=newpr;
}

Jul 23 '05 #5
an*****@hotmail.com wrote:
Hi all, I have a question about the code below. Any idea abuot that,
thanks in advance!

myclass.h
class myclass
{
public:
myclass();
~myclass();
void setdata();

private:
yclass *ypr;
};

myclass.cpp
include"myclass.h"
include"yclass.h"

myclass::myclass()
{
yclass *ypr=new yclass;
This is a HUGE mistake. You declare a _local_ variable 'ypr', which
_hides_ the member 'ypr'. You may think you are assigning the result
of using 'new' to the 'this->ypr', but you actually aren't. The
declaration of the member 'ypr' is in the class definition. Drop the
'yclass *' here if you actually want to assign to the member. Better
yet, use _initialisation_ instead (read about "initiliaser lists" in
your favourite C++ book).
}

myclass::~myclass()
{
delete ypr;
}
Clear violation of "the Rule of Three". Read about it in archives.
void myclass::setdata()
{
yclass *newpr=new yclass;
ypr=newpr; //My question is here. If I delete the "*ypr" pointer in
the destructor function of myclass, does the "*newpr" pointer being
deleted automatically since both of them have the same address?
They don't have the same address. The have the same value. The 'newpr'
local variable carries the value to 'ypr' and is then discarded. You
can forget 'newpr', it's superfluous anyway. You could simply write

ypr = new yclass;

here instead of the two statements.
the
newpr class object has the same lifetime as myclass(after it was
created), and I have no idea when it should be deleted.
There is no such thing as "newpr class object". 'newpr' is but
a pointer that has the value equal to the address of a dynamically
allocated 'yclass' object. You don't need to delete 'newpr' since
its value is assigned (and therefore owned) by 'ypr' in this function.
}


Let me make one more comment. OK, two. First, you need to understand
pointers before you attempt dynamic memory management. How you do it
is up to you. I suggest exploring addresses of real objects created
statically or automatically, but not dynamically. Pass the pointers
to functions, pass objects to functions, query the addresses of those
objects and the contents. See more books (than you have already seen).
Second, you have a memory leak in the 'setdata' member function. The
'ypr' pointer's old value that used to holds the address of another
dynamically allocated object, is lost when you assign another value to
it. So, the dynamic 'yclass' object, which you allocated in the
constructor, is hanging around forever, and you can't do anything about
it because you don't have its address any more.

V
Jul 23 '05 #6

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:d9*************@news.t-online.com...
So what you have to do is delete the old object within setdata:

void myclass::setdata()
{
yclass *newpr=new yclass;
delete ypr;
ypr=newpr;
}


Or just

void myclass::setdata()
{
delete ypr;
ypr = new yclass;
}

No need for a temporary.

-Howard
Jul 23 '05 #7
Howard wrote:
void myclass::setdata()
{
yclass *newpr=new yclass;
delete ypr;
ypr=newpr;
}


Or just

void myclass::setdata()
{
delete ypr;
ypr = new yclass;
}

No need for a temporary.


Well, it depends on how robust you want your code. Think what happens if the
operator new or yclass's constructor throws an exception.

Jul 23 '05 #8

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

Similar topics

2
by: lawrence | last post by:
I had some code that worked fine for several weeks, and then yesterday it stopped working. I'm not sure what I did. Nor can I make out why it isn't working. I'm running a query that should return 3...
5
by: lawrence | last post by:
I posted before, but have now narrowed my problem down to this method. At the start of the method, I test to make sure that I have a resource, a pointer to data returned from a database. This test...
2
by: Asfand Yar Qazi | last post by:
Hello. Partly for learning purposes, I have written a smart pointer class. Could you please tell me what's wrong with it? (I know there's something wrong with it, but just not what!) Note...
4
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived...
6
by: Itay_k | last post by:
Hello, I want a member in my class that will save pointer to pointer to System::Drawing::Image class. When I write on my class code: System::Drawing::Image **bmp; I get this error message:...
2
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
7
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
5
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
8
by: mathieu | last post by:
Hi there I have implemented a very simple smartpointer class (invasive design). And I was wondering what should be the natural API when using those in a Container. I choose to define the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.