PDA

View Full Version : C++ Help Please


LukeA
06-28-2004, 11:04 PM
Hi all.. I am in the middle of working on a program for a class and seeking help.. I came across these boards and from what I've read everyone here seems to be very helpful.. so to my problem....

I have a dynamic array of objects. I am making a function to add an object into the array. I have the output working from the function, but when I get back into main(), I receive an error message when trying to access the data within the object. So I am guessing my problem is with passing the array into the function, my prototype:
void createBaseball(Baseball baseball[], int& size);

And here is a bit of the code I am using to increase the size of original array:
Baseball* temp = new Baseball[size];
for (int i=0; i < size; i++)
temp[i] = baseball[i];

delete [] baseball;

size++;
baseball = new Baseball[size];

for (int j=0; j < size-1; j++)
baseball[j] = temp[j];

delete [] temp;

Any help on resolving this would be greatly appreciated.. thanks!

eak
06-29-2004, 03:23 AM
this sounds easy enough. its kinda what i learned in cs1411 in my first year in college. Since you did say it is for class the people here are not going to do it for you. We will help you figure it out though. Also, a full cpp source code listing would help out too...

LukeA
06-29-2004, 03:43 AM
Thanks for the reply.. I had everything running smoothly up until this point, a little help is all I need.. I don't expect, or want someone to do it for me, I want to figure out where my error for this and future reference. My .cpp file is fairly large, so I edited out functions and other parts that do not pertain to this problem:


//*********** Prototypes **************************
void fillBaseball(Baseball baseball[]);
void fillDriving(Driving driving[]);
void fillFPS(FPS shooter[]);

void outputSelection(Baseball baseball[], const int BASE_SIZE,
Driving driving[], const int DRIV_SIZE,
FPS shooter[], const int FPS_SIZE);

void createSelection(Baseball baseball[], int& base_size);
void createBaseball(Baseball baseball[], int& size);

void createDriving();
void createFPS();

void invalidSelection();

//*********** main *******************************
int main()
{

bool okay, quit(0);
char selection;
int base_size = 6;
int driv_size = 1;
int shooter_size = 1;

Baseball* baseball;
baseball = new Baseball[base_size];
fillBaseball(baseball); // fills baseball array of Baseball objects

Driving* driving;
driving = new Driving[driv_size];
fillDriving(driving); // fills driving array of Baseball objects

FPS* shooter;
shooter = new FPS[shooter_size];
fillFPS(shooter); // fills FPS array of Baseball objects

do
{

do
{
cout<< "\n\t1 - Display Objects"
<< "\n\t2 - Sort Objects"
<< "\n\t3 - Create Object"
<< "\n\t4 - Exit Program"
<< "\n\n\tPlease make your selection: ";
cin>> selection;
cin.ignore(100, '\n');
selection = toupper(selection);
okay = (selection >= '1' && selection <= '4');
if (! okay)
{
invalidSelection();
system("Pause");
}
}
while (! okay);

switch (selection)
{
case '1':
outputSelection(baseball, base_size, driving, driv_size, shooter, shooter_size);
break;
case '2':
//baseball[base_size-1].output();
break;
case '3':
createSelection(baseball, base_size);
break;
case '4':
quit = 1;
break;
}

} while (! quit);


cout<< endl << endl << "Goodbye!\n\n";
system("Pause");
return 0;
}

void createSelection(Baseball baseball[], int& base_size)
{
char selection;
bool okay;

do
{
cout<< "\n\t1 - Add a Baseball Game"
<< "\n\t2 - Add a Driving Game"
<< "\n\t3 - Add a First Person Shooter Game"
<< "\n\n\tPlease make your selection: ";
cin>> selection;
cin.ignore(100, '\n');
selection = toupper(selection);
okay = (selection >= '1' && selection <= '3');
if (! okay)
{
invalidSelection();
system("Pause");
}
}
while (! okay);

switch (selection)
{
case '1':
createBaseball(baseball, base_size);
break;
case '2':
createDriving();
break;
case '3':
createFPS();
break;
}

}

void createBaseball(Baseball baseball[], int& size)
{

string name, console, genre, type;
char char_online, char_type, char_roster, char_minors, char_derby;
bool okay, online, roster, minors, derby;
int players;
double price;

cout<< "Enter the Name of the game: ";
getline(cin,name);

cout<< "Enter the Console the game is for: ";
getline(cin,console);

cout<< "Enter the Genre of the game: ";
getline(cin,genre);


Baseball* temp = new Baseball[size];
for (int i=0; i < size; i++)
temp[i] = baseball[i];

delete [] baseball;

size++;
baseball = new Baseball[size];

for (int j=0; j < size-1; j++)
baseball[j] = temp[j];

delete [] temp;

baseball[size-1].setValues(name, console, genre, online, players, price, type,
roster, minors, derby);

for (int k=0; k < size; k++)
baseball[k].output();

}



And as stated in my original post, I can output the data in the object- as the last 2 lines of my code show- fine, but when I get back into main, attempting to output the data results in an error message when trying to access the first variable. Thanks again for your help.

eak
06-29-2004, 04:25 AM
hey,
i just looked over your code and I notice a few things. first of all when dynamically creating objects using pointers you can put it all on one line like this: Baseball* baseball = new Baseball[base_size];
the 2 line way will work but I guess it depends on your coding style for which one you like.
You dont have all the code there so i could not compile and debug it. I need to see the outputSelection function too see the error msg.

on a side note, why are all of your return types void? why not return a different object? where are your class definitions and what header files are you including?

LukeA
06-29-2004, 04:34 AM
Ok, sorry I didn't think about putting it into a compiler.. here is the complete .cpp with the class definitions.

/* Luke G Adams
June 23, 2004
COSC 201
Project 5
*/

//*********** Includes *********************************************
#include <iostream>
#include <iomanip>
#include <string>
#include <cassert>

using namespace std;


//*********** Game BASE Class ***************************************
class Game
{
public:
Game(string name_value, string console_value, string genre_value,
bool online_value, int players_value, double price_value);
Game() { };

protected:
string name;
string console;
string genre;
bool online;
int players;
double price;
};

Game::Game(string name_value, string console_value, string genre_value,
bool online_value, int players_value, double price_value)
{
name = name_value;
console = console_value;
genre = genre_value;
online = online_value;
players = players_value;
price = price_value;
}


//*********** Sports Class *****************************************
class Sports:public Game
{
public:
Sports(string name_value, string console_value, string genre_value,
bool online_value, int players_value, double price_value,
string type_value, bool roster_value);
Sports() { };
void output();

protected:
string type;
bool rosterUpdates;
};

Sports::Sports(string name_value, string console_value, string genre_value,
bool online_value, int players_value, double price_value,
string type_value, bool roster_value)
: Game(name_value, console_value, genre_value,
online_value, players_value, price_value)
{
type = type_value;
rosterUpdates = roster_value;
}


//*********** Simulation Class *************************************
class Simulation:public Game
{
public:
Simulation(string name_value, string console_value, string genre_value,
bool online_value, int players_value, double price_value,
bool content_value);
Simulation() { };
void output();

protected:
bool downContent;

};

Simulation::Simulation(string name_value, string console_value, string genre_value,
bool online_value, int players_value, double price_value,
bool content_value)
: Game(name_value, console_value, genre_value,
online_value, players_value, price_value)
{
downContent = content_value;

}


//*********** Action Class *****************************************
class Action:public Game
{
public:
Action(string name_value, string console_value, string genre_value,
bool online_value, int players_value, double price_value,
bool content_value, bool coop_value);
Action() { };
void output();

protected:
bool downContent;
bool coop;
};

Action::Action(string name_value, string console_value, string genre_value,
bool online_value, int players_value, double price_value,
bool content_value, bool coop_value)
: Game(name_value, console_value, genre_value,
online_value, players_value, price_value)
{
downContent = content_value;
coop = coop_value;
}


//*********** Baseball Class ***************************************
class Baseball:public Sports
{
public:
//Baseball(string name_value, string console_value, string genre_value,
// bool online_value, int players_value, double price_value,
// string type_value, bool roster_value, bool minors_value,
// bool derby_value);

Baseball() { /*Default Constructor*/ };
void setValues(string name_value, string console_value, string genre_value,
bool online_value, int players_value, double price_value,
string type_value, bool roster_value, bool minors_value,
bool derby_value);
void output();

private:
bool minors;
bool derby;

};

/*
Baseball::Baseball(string name_value, string console_value, string genre_value,
bool online_value, int players_value, double price_value,
string type_value, bool roster_value, bool minors_value,
bool derby_value)
: Sports(name_value, console_value, genre_value, online_value,
players_value, price_value, type_value, roster_value)
{
minors = minors_value;
derby = derby_value;
}
*/

void Baseball::setValues(string name_value, string console_value, string genre_value,
bool online_value, int players_value, double price_value,
string type_value, bool roster_value, bool minors_value,
bool derby_value)
{
name = name_value;
console = console_value;
genre = genre_value;
online = online_value;
players = players_value;
price = price_value;
type = type_value;
rosterUpdates = roster_value;
minors = minors_value;
derby = derby_value;
}


void Baseball::output()
{
cout<< setw(18) << "Name: ";
cout<< name << endl;

cout<< setw(18) << "Console: ";
cout<< console << endl;

cout<< setw(18) << "Genre: ";
cout<< genre << endl;

cout<< setw(18) << "Online: ";
cout<< online << endl;

cout<< setw(18) << "Roster Updates: ";
cout<< rosterUpdates << endl;

cout<< setw(18) << "Players: ";
cout<< players << endl;

cout<< setw(18) << "Type: ";
cout << type << endl;

cout<< setw(18) << "Minor Leagues: ";
cout<< minors << endl;

cout<< setw(18) << "Homerun Derby: ";
cout<< derby << endl;

cout<< setw(19) << "Price: $";
cout<< price << endl << endl;
}


//*********** Driving Class ****************************************
class Driving:public Simulation
{
public:
Driving() { /*Default Constructor*/ };
void setValues(string name_value, string console_value, string genre_value,
bool online_value, int players_value, double price_value,
bool content_value, bool split_value, bool drag_value);
void output();

protected:
bool splitScreen;
bool dragRacing;
};

void Driving::setValues(string name_value, string console_value, string genre_value,
bool online_value, int players_value, double price_value,
bool content_value, bool split_value, bool drag_value)
{
name = name_value;
console = console_value;
genre = genre_value;
online = online_value;
players = players_value;
price = price_value;
downContent = content_value;
splitScreen = split_value;
dragRacing = drag_value;
}

void Driving::output()
{
cout<< setw(18) << "Name: ";
cout<< name << endl;

cout<< setw(18) << "Console: ";
cout<< console << endl;

cout<< setw(18) << "Genre: ";
cout<< genre << endl;

cout<< setw(18) << "Online: ";
cout<< online << endl;

cout<< setw(18) << "Content Download: ";
cout<< downContent << endl;

cout<< setw(18) << "Players: ";
cout<< players << endl;

cout<< setw(18) << "Split Screen: ";
cout << splitScreen << endl;

cout<< setw(18) << "Drag Racing: ";
cout<< dragRacing << endl;

cout<< setw(19) << "Price: $";
cout<< price << endl << endl;
}


//*********** FPS Class ********************************************
class FPS:public Action
{
public:
FPS() { /*Default Constructor*/ };
void setValues(string name_value, string console_value, string genre_value,
bool online_value, int players_value, double price_value,
bool content_value, bool coop_value, bool vehicle_value,
int levels_value);
void output();

protected:
bool vehicles;
int levels;
};

void FPS::setValues(string name_value, string console_value, string genre_value,
bool online_value, int players_value, double price_value,
bool content_value, bool coop_value, bool vehicle_value,
int levels_value)
{
name = name_value;
console = console_value;
genre = genre_value;
online = online_value;
players = players_value;
price = price_value;
downContent = content_value;
coop = coop_value;
vehicles = vehicle_value;
levels = levels_value;
}

void FPS::output()
{
cout<< setw(18) << "Name: ";
cout<< name << endl;

cout<< setw(18) << "Console: ";
cout<< console << endl;

cout<< setw(18) << "Genre: ";
cout<< genre << endl;

cout<< setw(18) << "Online: ";
cout<< online << endl;

cout<< setw(18) << "Content Download: ";
cout<< downContent << endl;

cout<< setw(18) << "Players: ";
cout<< players << endl;

cout<< setw(18) << "2 Player Co-Op: ";
cout << coop << endl;

cout<< setw(18) << "Vehicles: ";
cout<< vehicles << endl;

cout<< setw(18) << "# of Levels: ";
cout<< levels << endl;

cout<< setw(19) << "Price: $";
cout<< price << endl << endl;
}

//*********** Output Objects ***************************************
template <class Item>
void output(Item data[], const int SIZE)
{
for (int i=0; i < SIZE; i++)
data[i].output();
}

//*********** Prototypes *******************************************
void fillBaseball(Baseball baseball[]);
void fillDriving(Driving driving[]);
void fillFPS(FPS shooter[]);

void outputSelection(Baseball baseball[], const int BASE_SIZE,
Driving driving[], const int DRIV_SIZE,
FPS shooter[], const int FPS_SIZE);

void createSelection(Baseball baseball[], int& base_size);
void createBaseball(Baseball baseball[], int& size);

void createDriving();
void createFPS();

void invalidSelection();

//*********** main *************************************************
int main()
{

bool okay, quit(0);
char selection;
int base_size = 6;
int driv_size = 1;
int shooter_size = 1;

Baseball* baseball;
baseball = new Baseball[base_size];
fillBaseball(baseball); // fills baseball array of Baseball objects

Driving* driving;
driving = new Driving[driv_size];
fillDriving(driving); // fills driving array of Baseball objects

FPS* shooter;
shooter = new FPS[shooter_size];
fillFPS(shooter); // fills FPS array of Baseball objects

do
{

do
{
cout<< "\n\t1 - Display Objects"
<< "\n\t2 - Sort Objects"
<< "\n\t3 - Create Object"
<< "\n\t4 - Exit Program"
<< "\n\n\tPlease make your selection: ";
cin>> selection;
cin.ignore(100, '\n');
selection = toupper(selection);
okay = (selection >= '1' && selection <= '4');
if (! okay)
{
invalidSelection();
system("Pause");
}
}
while (! okay);

switch (selection)
{
case '1':
outputSelection(baseball, base_size, driving, driv_size, shooter, shooter_size);
break;
case '2':
//baseball[base_size-1].output();
break;
case '3':
createSelection(baseball, base_size);
break;
case '4':
quit = 1;
break;
}

} while (! quit);


cout<< endl << endl << "Goodbye!\n\n";
system("Pause");
return 0;
}


void outputSelection(Baseball baseball[], const int BASE_SIZE,
Driving driving[], const int DRIV_SIZE,
FPS shooter[], const int SHOOTER_SIZE)
{
char selection;
bool okay;

do
{
cout<< "\n\t1 - Output Baseball Games"
<< "\n\t2 - Output Driving Games"
<< "\n\t3 - Output First Person Shooter Games"
<< "\n\n\tPlease make your selection: ";
cin>> selection;
cin.ignore(100, '\n');
selection = toupper(selection);
okay = (selection >= '1' && selection <= '3');
if (! okay)
{
invalidSelection();
system("Pause");
}
}
while (! okay);

switch (selection)
{
case '1':
output(baseball, BASE_SIZE);
break;
case '2':
output(driving, DRIV_SIZE);
break;
case '3':
output(shooter, SHOOTER_SIZE);
break;
}

}


void createSelection(Baseball baseball[], int& base_size)
{
char selection;
bool okay;

do
{
cout<< "\n\t1 - Add a Baseball Game"
<< "\n\t2 - Add a Driving Game"
<< "\n\t3 - Add a First Person Shooter Game"
<< "\n\n\tPlease make your selection: ";
cin>> selection;
cin.ignore(100, '\n');
selection = toupper(selection);
okay = (selection >= '1' && selection <= '3');
if (! okay)
{
invalidSelection();
system("Pause");
}
}
while (! okay);

switch (selection)
{
case '1':
createBaseball(baseball, base_size);
break;
case '2':
createDriving();
break;
case '3':
createFPS();
break;
}

}

void createBaseball(Baseball baseball[], int& size)
{

string name, console, genre, type;
char char_online, char_type, char_roster, char_minors, char_derby;
bool okay, online, roster, minors, derby;
int players;
double price;

cout<< "Enter the Name of the game: ";
getline(cin,name);

cout<< "Enter the Console the game is for: ";
getline(cin,console);

cout<< "Enter the Genre of the game: ";
getline(cin,genre);


do
{
cout<< "Does this game offer online play? (y/n) ";
cin>> char_online;
cin.ignore(100, '\n');
char_online = toupper(char_online);
okay = (char_online == 'Y' || char_online == 'N');
if (! okay)
{
invalidSelection();
system("Pause");
}
if (char_online == 'Y')
online = 1;
else
online = 0;
} while (! okay);


do
{
cout<< "What is the maximum number of players this game offers? (1-4) ";
cin>> players;
cin.ignore(100, '\n');
okay = (players >= 1 && players <= 4);
if (! okay)
{
invalidSelection();
system("Pause");
}
} while (! okay);


do
{
cout<< "Enter the price of this game: $";
cin>> price;
cin.ignore(100, '\n');
} while (! okay);


do
{
cout<< "Is the game type: 1 - Arcade Style or 2 - Simulation Style? ";
cin>> char_type;
cin.ignore(100, '\n');
okay = (char_type == '1' || char_type == '2');
if (! okay)
{
invalidSelection();
system("Pause");
}
if (char_online == '1')
type = "Arcade";
else
type = "Simulation";
} while (! okay);


do
{
cout<< "Does this game offer online roster updates? (y/n) ";

if (! online)
cout<< "N/A\n";
else
{
cin>> char_roster;
cin.ignore(100, '\n');
char_roster = toupper(char_roster);
okay = (char_roster == 'Y' || char_roster == 'N');
if (! okay)
{
invalidSelection();
system("Pause");
}
if (char_roster == 'Y')
roster = 1;
else
roster = 0;
}
} while (! okay);


do
{
cout<< "Does this have a Minor League system/rosters? (y/n) ";
cin>> char_minors;
cin.ignore(100, '\n');
char_minors = toupper(char_minors);
okay = (char_minors == 'Y' || char_minors == 'N');
if (! okay)
{
invalidSelection();
system("Pause");
}
if (char_minors == 'Y')
minors = 1;
else
minors = 0;
} while (! okay);


do
{
cout<< "Does this game offer a Home Run Derby? (y/n) ";
cin>> char_derby;
cin.ignore(100, '\n');
char_derby = toupper(char_derby);
okay = (char_derby == 'Y' || char_derby == 'N');
if (! okay)
{
invalidSelection();
system("Pause");
}
if (char_derby == 'Y')
derby = 1;
else
derby = 0;
} while (! okay);


Baseball* temp = new Baseball[size];
for (int i=0; i < size; i++)
temp[i] = baseball[i];

delete [] baseball;

size++;
baseball = new Baseball[size];

for (int j=0; j < size-1; j++)
baseball[j] = temp[j];

delete [] temp;

baseball[size-1].setValues(name, console, genre, online, players, price, type,
roster, minors, derby);

for (int k=0; k < size; k++)
baseball[k].output();

}

void createDriving()
{


}

void createFPS()
{

}

void fillBaseball(Baseball baseball[])
{
baseball[0].setValues("ESPN Major League Baseball", "X-Box", "Sports", 1, 4, 39.95,
"Simulation", 1, 1, 0);
baseball[1].setValues("MLB SlugFest: Loaded", "X-Box", "Sports", 1, 4, 39.95,
"Arcade", 1, 0, 1);
baseball[2].setValues("MLB 2005", "Playstation 2", "Sports", 1, 2, 39.95,
"Simulation", 1, 0, 1);
baseball[3].setValues("MVP Baseball 2004", "X-Box", "Sports", 1, 2, 49.95,
"Simulation", 1, 1, 1);
baseball[4].setValues("All-Star Baseball 2005", "X-Box", "Sports", 1, 4, 39.95,
"Simulation", 1, 0, 1);
baseball[5].setValues("High Heat Major League Baseball", "Playstation 2", "Sports", 0, 2, 29.95,
"Simulation", 0, 0, 1);
}

void fillDriving(Driving driving[])
{
driving[0].setValues("Need For Speed Underground", "X-Box", "Simulation", 0, 2, 49.95,
0, 1, 1);
}

void fillFPS(FPS shooter[])
{
shooter[0].setValues("Halo", "X-Box", "Action", 0, 2, 29.95,
0, 1, 1, 14);
}

//*********** invalidSelection **************************************
void invalidSelection()
{
cout<< "\nERROR: You have made an invalid selection. Please try again.\n";
}

I'm not sure why all my functions are void, I guess I didn't see another way of doing it.. Also, this is my first time working with inheritance and derived classes, so sorry if it looks kinda messy..

eak
06-29-2004, 05:30 AM
hey,
I can compile it with out any problems but i can get it to crash while it is running.

Have you thought about dumping your output to a txt file so you can review it later?
also, you should seperate your cpp file into several header files so you can find the problem easily. when I was doing projects I would end up having at least 5 header files with all of my classes/common functions in them.

when you press option 3 then 1 it has an error when asking for online play.
from what i can see, it looks like a problem I had with cin/getline. look into cin.ignore()...

another side note... why do you use template<class foo> when template<typename foo> works just the same and is the prefered method?

Since your using an inherited class structure, have you learned about polymorphism yet? if not, i will not confuse you with it.


I hated using arrays like this back in school. can you use vectors or any other stl container?

LukeA
06-29-2004, 06:07 AM
Hi again, thanks for looking over the file. I did not get the error you were referring to with online play. The only trouble I was having was after I goto option 3, then 1 to create a new object into the array, I can no longer view the objects outside of that function. It will let me view the entire array of objects within that function, which is why I was thinking it had to do with how I was passing the arrays into the function. I know you cannot pass arrays by reference, but you don't need to because the values should change by default, correct?

It is not specified what container type to use, I chose arrays because I have the most experience with them and thought it would be the easiest road to take.. I had multiple header files, I had just combined everything into one for the simplicity of posting the code.

I did not know of using <typename Item> instead of <class Item> as the prefered method. I will look into that.. the book I have directed me to <class Item>

As for polymorphism, we only touched upon this with using templates. Anything further I do not have much, if any knowledge of.

eak
06-29-2004, 03:22 PM
I know you cannot pass arrays by reference, but you don't need to because the values should change by default, correct?

arrays are passes by reference by default. you dont have to tell it to like you do with other data types.

I wasn't able to find the problem your having (too little time, too much to do...)
My only suggestion is to simplify your code. Take out every thing that you want to get working and make a seperate file for it. for example, copy and paste all the code needed to get the array copying / reading into another workspace and get it working before you finish the rest of your project.

LukeA
06-29-2004, 08:49 PM
Got it figured out.. I can't edit the size of the dynamic array within a function, when it sends it back to main the memory locations were getting screwed up somewhere along the line. I put the code to add an additional index in my array in main and got it working fine.

Thanks for your help eak.. appreciate it. :)

Unit
06-29-2004, 09:12 PM
Your problem is in this code..


Baseball* temp = new Baseball[size];
for (int i=0; i < size; i++)
temp[i] = baseball[i];

delete [] baseball;

size++;
baseball = new Baseball[size];

for (int j=0; j < size-1; j++)
baseball[j] = temp[j];

delete [] temp;


When you pass an array, a reference is passed to the function. Let me illustrate it this way.

Lets say you have a memory block M and a reference to it A
you call a function with this reference F(A)
the function call makes a copy of the reference. Lets call it B
Now you can modify M because B is a reference to it. But you cannot modify A because you have no access to it!
So if you modify B, you are modifying B. If you delete B, the memory M is freed making A an invalid reference. With me till now?

Now what you are doing in the function is exactly this. You are freeing memory for the Array which still has references outside this function. Then you are trying to associate new memory to the copy of the reference in the function which will be gone as soon as the function is finished. The memory is still there, but you lost all references to it. To overcome this either pass the reference to the reference of the array.. or use vector.. or make a list yourself.

Also, on a side note, you are allocating one object at a time when you need more space. This is very inefficient. If you are dealing with variable size Arrays, a better approach would be to allocate a bunch of objects whenever you hit a limit. For example, you find that you are out of objects, you allocate 5 new objects. I just picked 5 arbitrarily. Typically you would study the incoming behaviour and decide on the increment amount. Then you dont have to copy all the objects till you run out of space again. That way you limit the number of copies that are made and run faster.

Hope that helped.

Edit: Ah, you got it figured out while I was writing this post - glad that you got it working now.

LukeA
06-30-2004, 12:25 AM
Hey Unit thanks for the reply. With what your saying about efficiency, should I start out with the array larger than what I need, say 2 extra indexes? And then, if my user tries to go beyond that, add an additional index for each attempt?

Or would it be better to start out with the correct size, then increment like you said 5 additional places each time the user attempts to go beyond that capacity?

In my attempt to figure out the previous problem I overlooked trying to keep my program as effecient as possible.

Unit
06-30-2004, 01:02 AM
I just felt the need to point that little aspect.

Look at the stats
1. You have 100 objects already in the array.
You allocate once for each object and copy all the previous objects to the new array.
So, to add 10 objects now, you copy 1045 objects.

change the scenario to allocating space for 5 Objects whenever you go over the capacity. Lets assume we are at capacity now.
To add 10 objects now, you copy 205 objects.

As you can see, when you start using more and more objects, the savings become more even if your increment is quite small like 5 at a time.

To answer your question, start with a few more objects than you need and always allocate in batches of a few objects(5) when you go over the capacity.

To be as efficient as possible, you may have to ditch the idea of variable size array of objects and use other datastructures depending on your need. For example, you could store only the references to the objects in the array and not the objects themselves. This eliminates all copying of objects by copying only the references when needed. The savings in this scenario increase as your objects become large. You could also use Linked lists or the built in List class from STL.