|
-
Jan 27th, 2002, 04:18 AM
#1
Thread Starter
Addicted Member
pointers2pointers
Guys....this is a problem I'm facing with Pointers 2 Pointers...
Model: Suzy owns a kennel where she boards pets. The kennel building contains 20 stalls, and she usually has cats in half the stalls and dogs in the other half.
class Animal
{
protected:
char*name;
public:
Animal()
{
Name=NULL;
}
Animal(char* n)
{
Name=strdup(n);
}
~Animal()
{
delete Name;
}
void WhoAmI()
{
cout<<"Generic Animal";
}
};
class Cat: public animal
{
public:
Cat()
Cat(char* n):Animal()
{//empty}
void WhoAmI(){cout<<"Cat Named"<<name;}
};
Class Dog ublic Animal
{
//similar Code 2 Cat Class
};
class Kennel
{
private:
unsigned int MaxCats;
unsigned int NumCats;
Cat ** Kitties;//Now here's the problem
unsigned int MaxDogs;
unsigned int MaxCats;
Dog ** Doggies;
public:
Kennel(unsigned int maxc,unsigned int maxd);
~Kennel();
unsigned int Accept(Dog *d);
unsigned int Accept(Cat* c);
Dog * ReleaseDog(unsigned int pen);
Cat * ReleaseCat(unsigned int pen);
void ListAnimals();
};
Kennel::Kennel(unsigned int maxc,unsigned int maxd)
{
MaxCats=maxc;
MaxDogs=maxd;
NumCats=0;
NumCats=0;
Kitties = new Cat*[MaxCats];
Doggies = new Dog*[MaxDogs];
for(int i= 0;i<MaxCats;++i)
Kitties[i]=NULL;
for(i=0;i<MaxDogs;++i)
Doggies[i]=NULL;
}
Author's Xplanation:..A Kennel object contains pointers to 2 arrays,one containing pointers to Dogs..and the other containing pointers to Cats.Data Members track the size of the array and how many spots in each array actually contain Animals.
Now..my limited knowledge of Pointers and Pointers2Pointers was turned upside down in finding an Xplanation to what is being done here with regards to the pointer2pointers which the author regards as a Pointer to an array of Pointers which Point to Dogs.....help needed..and if possible..any URLs on Pointers to Pointers..
-
Jan 27th, 2002, 12:31 PM
#2
IMHO, this code is poorly written (that's a polite version of saying "it sucks"). Since the author is already using C++, he should use string for the name instead of char*. Then he wouldn't have to worry about deleting it. The code would produce an error if I do
Code:
{ // any block, maybe of an if
Animal *****; // should not, but could be
} // error here
This is because he doesn't check for NULL when deleting name in the destructor. Just as bad is that he uses delete instead of delete[] on an array of chars (C-string). Not only that, but he KNOWS that at least at times the memory is allocated using malloc. (strdup uses malloc), which you shouldn't mix with new/delete. Also, he doesn't forward the parameter to the cat/dog constructor to the animal constructor.
Pointers2Pointers:
Since a pointer is also only a variable, you can have pointers pointing to them. And you can have an array of pointers. The author wants a lot of Cats and Dogs so he stores them in an array. But there is not always the maximum number of animals so in order to save space he decides to allocate them dynamically. This means he can't store it in objects but has to use pointers. You have an array of pointers.
char* ptrarr[SIZE];
But the author doesn't know the size of the array at compile time, so he allocates it dynamically too. He needs a pointer to hold the result of new.
char * * ptrarr;
You have a pointer to an array of pointers.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 28th, 2002, 12:32 AM
#3
Thread Starter
Addicted Member
guys...I'm a bit thick...so pls bear with me.....This is what I have finally understood from the whole thing....I'm sure I have gone wrong somewhere...please correct me
lemme just clarify this....suppose we have a data structure like
class person
{
int name; //2 bytes
int age; //2 bytes
char address[10]; //10 bytes
public:
//and so on....
}
total size of an object of class person would be 2+2+10=14
Now we have to store records of a 100 persons.
so the first option v have is
1) person parr[100]
but this is undesirable since it will reserve 14*100=1400 bytes in memory
Second op.:
2) person *parr[100]
this is kindof ok..but still not very good
since it will reserve ...what....I guess 2*100=200 bytes in memory...(...pointer is stored internally as integer????..)
But we want to make it even better...so:
3)person** ptr;
will not reserve any memory....until new operator is called...
so this is the best option ...right???
-
Jan 28th, 2002, 09:20 AM
#4
int is 4 bytes in 32-bit environment -> Person is 18 bytes
If you want a 1-dimensional array of 20 entries:
a)
Person arP[100];
100*18 bytes = 1800 bytes. That's not terrible, but also not good, especially when you don't always need 100 persons.
b)
Person *arP;
// allocate
arP = new Person[Size];
Good. This takes up 4 bytes on the stack. When allocating, you need Size*18 bytes + allocation information on the heap. That's the best solution. It's easy to resize and doesn't take up too much memory.
c)
Person **arP;
// allocate the first part
arP = new Person*[Size];
// allocate the second part
arP[index] = new Person;
Unnecessary. This takes up 4 bytes on the stack. The first part is when you know you many persons you'll need at max. It takes up Size*4 bytes + alloc info on the heap. When you really need a place, you allocate again: 18 bytes + alloc info. Unless Person needs more than 100 bytes of memory this doesn't make sense. It's hard to keep track of all the pointers and very probable that you get memory leaks or access violations. The good part is that you have the most control of how much memory is used, but you waste memory for the additional poiners. I wouldn't do such a thing. It's easier to resize a normal dynamic array.
Person *arP[100];
// allocate
arP[index] = new Person;
This is not much better. You have only one layer of pointers (only 100 ), but it doesn't save much. This takes up 400 bytes on the stack.
To accomplish what this does, I would use a single/double linked list (STL container "list" or "slist"). It's absolutely perfect: The animals are added only at one end, are only searched sequentially and removed anywhere. It would free you from all the hard pointer work.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|