Skip to main content
GameDev.net gamedev.net
🔒 Locked

*new class

Started by Prod Jul 22, 2002 at 12:32 AM 3 replies 300+ views
Original Post
Prod
Prod
I have been writing a program that simulates population growth (for my AI), And i am having some problems with the the dynamic memory. *code is cut down*
    
//First i start off like this

cPerson *person[1];

//The program will not start when i leave this code uncomminted

int Male = NULL;
int Female = NULL;

for (int i = 0; i < Population; i++)
{
if (person[i]->bGender == true) Male = i;
if (person[i]->bGender == false) Female = i;
}

//This is how i create a person

Population = Population + 1;
person[Population] =  new cPerson();

//I think there is an error on how i delete also since a few

//seconds after the program quites i get a windows error from

//the program

void Death(cPerson guy)
{
	if(guy.Age >= 100) delete[] &guy
	Population = Population - 1;
}

void Kill(cPerson guy)
{
	delete[] &guy
	Population = Population -1;
}
    
Any help would be very appreciated [edited by - Prod on July 22, 2002 1:35:10 AM]
nicho_tedja
nicho_tedja
I were the compiler, I would write an error message for you:
"Undefined identifier: Population"

Although you might have defined this somewhere else, you still have some dangerous pitfall. Notice this fragment of code below:

cPerson *person[1];

It means you''re declaring two pointers of type cPerson, person[0] and person[1]. But, you put a "bomb" in your code:

person[Population] = new cPerson();

see the Population? It could be 0, 1, 2, 3, ..., 10281, or perhaps negative. Do you think the compiler knows what person[10281] is? You only declared 2 pointers, but you''re trying to make and use more than that, so you get a runtime error.

And your Death and Kill functions need some modifications:

  
void Death(cPerson* guy) {
if(guy->Age >= 100) delete[] guy;
Population = Population - 1;
}

void Kill(cPerson* guy) {
delete[] guy;
Population = Population -1;
}



My compiler generates one error message: "Doesn''t compile."
-Albert Tedja-
My compiler generates one error message: "does not compile."
nyStagmus
nyStagmus
As nicho_tedja pointed out you should declare person as
cPerson **person;
And initialize it with something like this
person = new cPerson*[MAX_POPULATION_SIZE];

Also, pass along a pointer to the kill and death functions like he showed you. I am not sure why you use delete[] though. Since you did a new cPerson() you should use only delete.

And be sure to initialize Population.

Why do you do this?
for (int i = 0; i < Population; i++)
{
if (person->bGender == true) Male = i;
if (person[i]->bGender == false) Female = i;
}
Dactylos
Dactylos
quote:
Original post by nicho_tedja
cPerson *person[1];

It means you''re declaring two pointers of type cPerson, person[0] and person[1]. But, you put a "bomb" in your code:


No, it means (s)he''s declaring a *one* element array of pointers to cPerson. Only person[0] is a valid memory location, not person[1].
Prod
Prod
Hey thanks for the help!

Hehehe, i guess thats the end of my midnight programming

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.