943,832 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3363
  • C++ RSS
Aug 18th, 2004
0

Constructor and Convertion operator are the same,how to avoid memory leak?

Expand Post »
Hi
I created a class,simplified class is like below:

#include<iostream.h>

class cCat
{
public:
//Constructors and Destructors
cCat();
~cCat();

//Accessor Function
int & cCat::GetAge() const{ return *itsAge;}

//Convertion operator
cCat(int); // cCat object =int x

private:
int *itsAge;
};

cCat::cCat()
{
itsAge = new int;
*itsAge= 20;
}
cCat::~cCat()
{
}

//Convertion Operator
cCat::cCat(int Age)
{
itsAge= new int;
*itsAge=Age;
}

main()
{
cCat MyCat(23);
cout<<"My Cat age:"<<MyCat.GetAge();
MyCat=42; //this makes a memory leak

return 0;
}

As the code shows MyCat(23) and MyCat=42 statements have the same definition in the class and this makes a memory leak.(cause without deleting previous allocated memory I allocate a new memory in cCat(int Age)

so,how can I avoid this memory leak?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
the_one2003a is offline Offline
13 posts
since Aug 2004
Aug 18th, 2004
0

Re: Constructor and Convertion operator are the same,how to avoid memory leak?

Heck, your destructor causes a memory leak too. You should delete age there.

There may be good reasons for a pointer to int here, but in this simple example an int would work fine, wouldn't it? But, assuming there's more to this....

= is an assignment operator, so it doesn't go through the constructor code. Since you don't define an assignment operator, the compiler figured out that it could turn the int into a cCat by calling the constructor, and then it could just copy en-masse the bytes over. Another leak!

Add an assignment operator:

cCat& operator = ( int age ) { *itsAge = age; return this; }

(I didn't try this out in the compiler, but you can get the syntax right when you do)

Remember that in an assignment operator, the object has already been constructed. As opposed to a copy constructor, in which the object has not already been constructed. The compiler will supply a default if it can, and the default will NOT be sensitive to allocated ram.
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Aug 19th, 2004
0

Re: Constructor and Convertion operator are the same,how to avoid memory leak?

Hi
Thanks for your help

I used a reference to int cause my int doesn't go out if scope after returning of my function so I tried to do this to avoid making a temporary int , and program be faster ,If I am wrong tell me ,I would be happy to know.

and about destructors, I saw some codes of a book that where written like this (I in destructors no delete wasn't suppied).
I think that it wouldn't make a memory leak cause my objects stay alife to end of the program and at end of it memory will become freed automatically

except when I create a pointer to my class,and allocate memory for it
are these corrrect?

thanks for your helps again
Reputation Points: 10
Solved Threads: 0
Newbie Poster
the_one2003a is offline Offline
13 posts
since Aug 2004
Aug 19th, 2004
0

Re: Constructor and Convertion operator are the same,how to avoid memory leak?

About the ptr to int/ref to int thing: the memory allocated for the class instance of cCat holds, in your case, a pointer. But holding an int is really the same in terms of what happens to the CLASS instance; if you return a cCat from a function the out-of-scope stuff still applies whether a member of the class is a pointer or not.

So, in this case, I think an int is perfectly adequate. If you had issues with returning the cCat from a routine, you might want to do a new on the whole cCat object, and return a pointer to the cCat. Like this:
C++ Syntax (Toggle Plain Text)
  1. cCat* GetACat(int age)
  2. {
  3. return new cCat(age);
  4. }
and then the caller has to 'delete' the return value when she's done. Generally, it would be easier to use something like this, unless there is a benefit for the allocated memory:
C++ Syntax (Toggle Plain Text)
  1. void GetCat( cCat* myCat, int age )
  2. {
  3. myCat->SetAge(age);
  4. }
  5. - or -
  6. void GetCatByRef( cCat& myCat, int age )
  7. {
  8. myCat.SetAge(age);
  9. }
  10.  
  11. To use, you would do something like this:
  12.  
  13. cCat myCat; // local space, not allocated
  14. GetCat( &myCat, 23 ); // in the first version
  15. GetCatByRef( myCat, 23 ); // in the second version

As to the destructor, you are correct that, in this specific case, the memory is freed when you exit your program. Though, when you originally mentioned a leak, the same thing would apply. You could leak a TON of memory and that would be ok since you exit the program.

Still, leaking ram is like running a stop sign. Someday you will get caught. Get in the habit of watching out for leaks and that good habit will save your keester someday.

Generally, if you allocate space in your class with new, you will want to:
a) set the pointer to NULL in your constructor, and
b) delete the pointer in your destructor, and
c) watch out for the assignment operator (do not let the compiler generate one for you, it will be wrong), and
d) make a copy constructor that allocates ram for the copy (again the compiler will be happy to generate the WRONG code for you)

If you put somthing in your destructor, you should also get in the good habit of declaring the destructor 'virtual' unless you have a clear reason for not doing that. Good reasons might include a tiny object that will NEVER have a subclass and there will be MILLIONS of them, so the saved pointer is truly a benefit.

a virtual destructor in this case also has no benefit yet, but if you do it as rote you won't make a mistake of that kind in a larger program where someone makes a subclass called 'cKitten' and you have a leak again.

Just sage advice.
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Aug 20th, 2004
0

Re: Constructor and Convertion operator are the same,how to avoid memory leak?

Thanks chainsaw for your great advices
I think that it would be better to use int instead of int & as your told
and also thanks for your nice adivces in using Pointer and declaring Constuctors and destructors.

Thanks again

Sincerely
the_one2003a
Reputation Points: 10
Solved Threads: 0
Newbie Poster
the_one2003a is offline Offline
13 posts
since Aug 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: this shall be my last stupid q about progamming
Next Thread in C++ Forum Timeline: Help with Classes





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC