What happens when you don't know how much storage to set aside? *
Do you just keep track of how many elements you store then if it over runs the array you write them to file and re-initialize. That sounds too complicated and slow.
Well in C I would do it like:
Code:
typedef (what ever) Item;
Item* ItemList;
int numItems = 0;
int maxItems = 0;
int addItem (Item item) {
*if (numItems >= maxItems -1) {
* *maxItems += 10;
* *ItemList = (Item*)realloc (ItemList, maxItem * sizeof (Item));
* *if (!ItemList) *
* * *return -1;
*}
*ItemList[numItems] = item;
*numItems++;
*return 0;
}
But since this is C++ I would use the <vector> facility of STL, then you have your pop/push functions like:
Code:
vector <what ever> my_vector_array;
my_vector_array.push(new_item);
Bookmarks