Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 C++ problem, C++

views
     
TSlohmk
post Feb 17 2006, 02:21 AM, updated 20y ago

Getting Started
**
Junior Member
234 posts

Joined: Jan 2003
From: Penang, Malaysia



These codes are found from a text by Jesse Liberty, which i find it very hard to understand..

CODE


#include <iostream.h>

class Cat
{
public:
Cat() { itsAge = 1; itsWeight = 5; }
~Cat();

int GetAge() const { return itsAge; }
int GetWeight() const { return itsWeight; }
void SetAge(int age) { itsAge = age; }

private:
int itsAge;
int itsWeight;
};

Cat::~Cat()
{
// cout << "Destructor called!\n";
}

int main()
{
Cat * Family = new Cat[10];
int i;
Cat * pCat;

for (i = 0; i < 10; i++)
{
 pCat = new Cat;
 pCat->SetAge(2*i + 1);
 Family[i] = *pCat;
 delete pCat;
}

for (i = 0; i < 10; i++)
{
 cout << "Cat #" << i+1 << ": ";
 cout << Family[i].GetAge() << endl;
}

delete [] Family;

return 0;
}



The author said that it is necessary to free the pointer after using it, to avoid creating memory leaks. My question is, after deleting Family, how am i gonna access the array on the free store again? Is it a wise move to release the Family pointer? Since then, if its not accessable again why not remove the whole array instead?

This post has been edited by lohmk: Feb 17 2006, 02:41 AM
koochy_rat
post Feb 17 2006, 10:16 AM

Getting Started
**
Junior Member
278 posts

Joined: Dec 2004


QUOTE(lohmk @ Feb 17 2006, 02:21 AM)
The author said that it is necessary to free the pointer after using it, to avoid creating memory leaks. My question is, after deleting Family, how am i gonna access the array on the free store again? Is it a wise move to release the Family pointer? Since then, if its not accessable again why not remove the whole array instead?
If you want to do anything to your Family array, you should do it before the delete[] operation. The delete[] operator will free up the whole array, that means all the memory taken up by your 10 Cats.
anthony_yio
post Feb 17 2006, 11:38 AM

........
Group Icon
Elite
1,828 posts

Joined: Jan 2003


QUOTE(lohmk @ Feb 17 2006, 02:21 AM)
These codes are found from a text by Jesse Liberty, which i find it very hard to understand..

CODE


#include <iostream.h>

class Cat
{
public:
Cat() { itsAge = 1; itsWeight = 5; }
~Cat();

int GetAge() const { return itsAge; }
int GetWeight() const { return itsWeight; }
void SetAge(int age) { itsAge = age; }

private:
int itsAge;
int itsWeight;
};

Cat::~Cat()
{
// cout << "Destructor called!\n";
}

int main()
{
Cat * Family = new Cat[10];
int i;
Cat * pCat;

for (i = 0; i < 10; i++)
{
 pCat = new Cat;
 pCat->SetAge(2*i + 1);
 Family[i] = *pCat;
 delete pCat;
}

for (i = 0; i < 10; i++)
{
 cout << "Cat #" << i+1 << ": ";
 cout << Family[i].GetAge() << endl;
}

delete [] Family;

return 0;
}



The author said that it is necessary to free the pointer after using it, to avoid creating memory leaks. My question is, after deleting Family, how am i gonna access the array on the free store again? Is it a wise move to release the Family pointer? Since then, if its not accessable again why not remove the whole array instead?
*
It is a redundant dynamic allocation in


Cat * pCat;

for (i = 0; i < 10; i++)
{
pCat = new Cat;
pCat->SetAge(2*i + 1);
Family[i] = *pCat;
delete pCat;
}

It show the coder do not understand why it the dynamic allocation is needed in the first place.

A normal declaration like Cat c already suffice.

for (i = 0; i < 10; i++)
{
Cat a
a.SetAge(2*i + 1);
Family[i] = a;
}

in fact, there isn't even a need to use dynamic array. The below work the same and faster and no memory fragmentation.

int main()
{
Cat Family[10];
int i;


for (i = 0; i < 10; i++)
{

Family[i].SetAge(2*i + 1);

}

for (i = 0; i < 10; i++)
{
cout << "Cat #" << i+1 << ": ";
cout << Family[i].GetAge() << endl;
}






nxfx
post Feb 17 2006, 01:02 PM

Enthusiast
*****
Senior Member
979 posts

Joined: Jan 2003


I think the coder is showing usage of pointer rather than the functionality of the program.
dstl1128
post Feb 17 2006, 02:11 PM

Look at all my stars!!
*******
Senior Member
4,464 posts

Joined: Jan 2003
>>The author said that it is necessary to free the pointer after using it, to avoid creating memory leaks.
Obviously... ... kind of correct.

>>My question is, after deleting Family, how am i gonna access the array on the free store again?
You use it before you delete it (which shown in the code as well).

>>Is it a wise move to release the Family pointer?
Wise or not, you should release anything when you no longer need it unless it might be used again in a very short coming future.

Since then, if its not accessable again why not remove the whole array instead?
What is not accessible? The example "delete[] family" in the end.

 

Change to:
| Lo-Fi Version
0.0148sec    1.52    5 queries    GZIP Disabled
Time is now: 24th December 2025 - 10:14 PM