array of class instances

I have an array of instances of a class. I initialize a limit of 5 because i want a maximum of 5 of them at one time. I want to be able to, when a new one is to be created, store it to the array in the first avaible spot. The problem is I don't have much experience in that regard. So the part I need help with is:
1) do I store the instance to the array, or a pointer of it, or does it matter.
2) how do I check to see which one is the first empty, I'm thinking using a while loop, but I don't know what it would be checked against

I think that covers it, I appreciate your help, and if anymore information is needed I will provide.
[665 byte] By [pyrotechnic] at [2007-11-20 11:23:29]
# 1 Re: array of class instances
You could do it either way, with pointers or instances, although instances isn't quite the proper term. What you're doing is filling up an array of memory slots for your structure. If your class structures are large, then you might prefer pointers to save memory.

To determine if a slot is empty, you can set one of the parameters in the structure to zero, or you could include an extra structure, which would hold a variable to indicate if a slot is empty. With pointers you could just set the pointers to NULL if the slot is empty.

The instance, or structure approach:

class myClass{
int var1;
int empty;
};

myClass array[5], zero;
zero.var1 = 0;
zero.empty = 0;

//initialize
for(int i=0;i<5;i++){
array[i] = zero;
}

//add entry
for(int i=0; i< 5;i++){
if(array[i].empty == 0){
array[i].var1 = 12;
array[i].empty = 1;
break;
}
}

Then, when you want to add a value, you iterate through each slot until you find an empty one and stick your stucture in.


Using pointers

myClass *pArray[5];

//initialize
for(int i=0;i<5;i++){
pArray[i] = NULL;
}

//Then, to add a value
for(int i = 0; i< 5; i++){
if(pArray[i] == NULL){
pArray[i] = new myClass();
pArray[i]->var1 = 10;
break;
}
}

In this case you will have to delete each element in the array when you're done with them to prevent a memory leak.

Hope that's clear.

You can also rate my post if it was helpful. Thanks.
jalway at 2007-11-9 1:25:36 >
# 2 Re: array of class instances
If you are going to be creating/deleting instances, it would be easier if you held then in a vector. If you dont know, a vector is basicly an array with a lot of cool functions, like add, delete, insert, ect.

http://www.cppreference.com/cppvector/index.html

They can be accessed with [] just like an array, or iterators if u want to do it more efficiently.
bovinedragon at 2007-11-9 1:26:29 >
# 3 Re: array of class instances
Thanks to both of you for your time.
I will address jalway first, thank you for correctly my vocabulary, although I was aware of that to begin with, I typed the topic up very quickly because I started to become frustrated with my attempts and just wanted an answer as quick as possible. I think I will use the pointer method, but I forgot how to manage it properly, like if I'm done with the class how do I get rid of it, for lack of a better term, so that there isn't a memory leak. Also, that was more or less the approach I was trying, I was forgetting to store the pointer and setting them to NULL first. I might have a bit more problems when I go to actually do it though, because I'm new to c++ in the sense of I know the syntax and howto's for the most part but havent had much implementation practice.
Now for bovinedragon, thanks for that information, I believe a vector, which i didn't know what it was before, is the best fit but I think its best if I go to them with a little more experience, if you don't agree, you probably know better.
Again, thanks for the help in a matter which is probably very simple, I will rate both of your posts soon, when i get the time, as they were extremely helpful.
pyrotechnic at 2007-11-9 1:27:38 >
# 4 Re: array of class instances
Thanks to both of you for your time.
I will address jalway first, thank you for correctly my vocabulary, although I was aware of that to begin with, I typed the topic up very quickly because I started to become frustrated with my attempts and just wanted an answer as quick as possible. I think I will use the pointer method, but I forgot how to manage it properly, like if I'm done with the class how do I get rid of it, for lack of a better term, so that there isn't a memory leak.

Well, after you create an array of pointers to object instances and you're done using them, you want to delete them.

To delete them you just iterate through the array and delete one after the other like this.

for(int i=0;i<5;i++){
if(pArray[i] != NULL){
delete pArray[i];
pArray[i] = NULL;
}
}
jalway at 2007-11-9 1:28:38 >
# 5 Re: array of class instances
Again, thanks. I believe that covers everything I needed.
pyrotechnic at 2007-11-9 1:29:40 >