vendredi 7 mars 2014

Access violation char [16] using linked list


Vote count:

0




Hi i have this linked list that after about 50 element crash and give me an access violation error



#include "stdafx.h"
#include "winsock.h"
#include "windows.h"
#include <string.h>
#include <iostream>
#include <Windows.h>
#include "Sample.h"

using namespace std;

class BUNNY
{
public:
int sex;
int name;
int age;
int colour;
bool radioactive_mutant_vampire_bunny;
BUNNY *nextBunny;
public:
void aging(BUNNY * head);
}bn;


int returnSex();
int returnName (int sex);
int returnColour ();
bool radioactiveBunny ();

bool IsOldEnough( BUNNY * head, int * MaleBunny,int * FemaleBunny);
bool IsTooOld(BUNNY * head,int * NumberOfDead);

string translateName(int name);
string translateSex(int sex);
string translateColour(int colour);
string translateRadioactive(bool radioactive);

BUNNY * AddBunny(BUNNY * head,int sex,int name,int colour,bool radioactive_mutant_vampire_bunny);
BUNNY * travereseBunny(BUNNY * head);
BUNNY * createBunny(BUNNY * head);


int _tmain(int argc, _TCHAR* argv[])
{
int NumberOfDead = 0;
bool flag = true;
int turn = 0;
BUNNY * head = NULL;
int FemaleBunny = 0;
int MaleBunny = 0;

while (flag)
{
++turn;
bn.aging(head);
if (turn == 1)
{
for (int i = 1; i <= 5; i++)
{
head = createBunny(head);
}
}else
{
head = createBunny(head);
}
if (IsOldEnough(head,&MaleBunny,&FemaleBunny))
{
for(int i = 0; i < FemaleBunny; i++)
{
printf("FROM TWO BUNNY A NEW BUNNY IS BORN!\n");
head = createBunny(head);
}
}
if(IsTooOld(head,&NumberOfDead))
{
printf("%d bunny are dead of old age \n ",NumberOfDead);
}

Sleep(1000);
}

return 0;
}

int returnSex()
{
int random = rand() % 100 + 1;
if(random < 50)
return MALE;
else if(random > 50)
return FEMALE;
}

int returnName(int sex)
{

if (sex == MALE)
{
int random = rand() % 100 + 1;
if(random < 20)
return BU;
else if (random > 20 && random < 40)
return CRYSTAL;
else if(random > 40 && random < 60)
return JASON;
else if(random > 60 && random < 80)
return ERO;
else if(random > 80)
return METH;
}
else
{
int random1 = rand() % 100 + 1;
if (random1 < 50 )
{
return MARIA;
}
else if (random1 > 50)
return JAMIE;
}
}

int returnColour ()
{
int random = rand() % 4 + 1;
if(random == 1)
return WHITE;
else if(random > 1 && random <= 2)
return BROWN;
else if(random > 2 && random <= 3)
return BLACK;
else if(random > 3 && random <= 4)
return SPOTTED;
}

bool radioactiveBunny()
{
int random = rand() % 100 + 1;
if(random > 0 && random <= 2)
return true;
else
return false;
}

BUNNY * AddBunny(BUNNY * head,int sex,int name,int colour,bool radioactive_mutant_vampire_bunny)
{
BUNNY * newBunny = new BUNNY;
newBunny->age = 0;
newBunny->colour = colour;
newBunny->sex = sex;
newBunny->name = name;
newBunny->radioactive_mutant_vampire_bunny = radioactive_mutant_vampire_bunny;

newBunny->nextBunny = head;
return newBunny;
}

string translateName (int name)
{
switch (name)
{
case CRYSTAL: return "CRYSTAL";
break;
case BU: return "BU";
break;
case JASON: return "JASON";
break;
case ERO: return "ERO";
break;
case METH: return "METH";
break;
case MARIA: return "MARIA";
break;
case JAMIE: return "JAMIE";
break;
}
}

string translateColour (int colour)
{
switch (colour)
{
case WHITE: return "WHITE";
break;
case BLACK: return "BLACK";
break;
case BROWN: return "BROWN";
break;
case SPOTTED: return "SPOTTED";
break;
}
}

string translateSex (int sex)
{
switch (sex)
{
case MALE: return "MALE";
break;
case FEMALE: return "FEMALE";
break;
}
}
string translateRadioactive(bool radioactive)
{
switch(radioactive)
{
case true: return "It's radioactive";
break;
case false: return "It's not radioactive";
break;
}
}

void BUNNY::aging(BUNNY * head)
{
for(BUNNY * i = head; i != NULL; i=i->nextBunny)
{
i->age +=1;
}
}
bool IsOldEnough( BUNNY * head, int * MaleBunny,int * FemaleBunny)
{
for(BUNNY * i = head; i!=NULL; i = i->nextBunny)
{
if(i->sex == MALE)
{
if(i->age >= 2)
{
*MaleBunny +=1;
}
}else if(i->sex == FEMALE)
{
if(i->age >= 2)
{
*FemaleBunny +=1;
}
}

}
if(*MaleBunny != 0 && *FemaleBunny != 0)
return true;
else
return false;
}

BUNNY * createBunny (BUNNY * head)
{
BUNNY * newBn = new BUNNY;
int sex = returnSex();
int name = returnName(sex);
int colour = returnColour();
bool radioactive = radioactiveBunny();
newBn = AddBunny(head,sex,name,colour,radioactive);
printf("A new bunny is born: sex %s , name %s colour %s %s\n",translateSex(sex).c_str(),translateName(name).c_str(),translateColour(colour).c_str(),translateRadioactive(radioactive).c_str());
return newBn;
}
bool IsTooOld(BUNNY * head,int *NumberOfDead)
{

for(BUNNY * i = head; i != NULL; i = i->nextBunny)
{
if(i->age == 10)
*NumberOfDead +=1;
}
if(*NumberOfDead > 0)
return true;
else
return false;
}


Watching at the debugger it says that is an overflow of a type char[16] buffer... what's going on? i think there is some other memory leak around here but i can't figure out where... thanks for your time.



asked 59 secs ago






Aucun commentaire:

Enregistrer un commentaire