Hi, I'm trying to do the last excercise of c++ beginner exercises.
I'm gettingI read a tutorial about linked lists and my understanding is that as far as you don't read/write beyond the last link, everything should be fine. Why does it do that?Code:
Unhandled exception at 0x774a15de (ntdll.dll) in Graduation.exe: 0xC0000005: Access violation writing location 0x00000000.
Here is my code(void new_bunnies seems to be causing the problem). The exception doesn't occure at every excecution.
Here is the snippet of code where visual studio points me after the exception.Code:#include <iostream> #include <time.h> #include <string> enum sex1{male, female, vampire}; std::string name1[]={"Acanthus","Ambrosia","Aria","Atiyana","Avalon","Azalea","Baylyn","Braelyn","Cadence","Calliope","Cashmere","Cassia", "Cayenne","Chavonne","Fauna","Flint","Forest","Forsythia","Glimmer","Harmony","Jescha","Keveen","Lark","Lilac","Meadow","Osment", "Quintessence","Rontae","Serendipity","Serenity","Slate","Sonnet","Taffeta","Temperance"}; std::string color[]={"white", "brown", "black", "spotted"}; struct bunny{ public: sex1 sex; std::string color; std::string name; int age; bool radioactive_mutant_vampire_bunny; int alive; //false 0 bunny* next; int number_of_bunny; }; bunny* root; bunny* conductor; int n,r; std::string female_color[700]; int k; int mutant_count; int number_of_bunnies=0; void create_bunny(); void create_5_bunnies(); void up_age(); void new_bunnies(); void create_bunny(); void kill_old(); int main() { srand(time(NULL)); int bun=0; std::cout<<"male=0;female=1;vampire=2\n"; create_5_bunnies(); while(number_of_bunnies<1000){ std::cout<<"NEW_BUNNIES"; new_bunnies(); std::cout<<"UP_AGE"; up_age(); std::cout<<"\nKILL_OLD\n"; kill_old(); std::cout<<number_of_bunnies<<" bunnies\n"; if(number_of_bunnies==0){ std::cout<<"All bunnies died"; number_of_bunnies=1000; bun=1; } } if(bun!=1){std::cout<<"Bunny population exceeded 1000";} std::cin.get(); return(0); } void create_5_bunnies(){ root=new bunny; conductor=root; for(n=1;n<(5+1);n++){ number_of_bunnies++; r=rand()%2+1; if(r==1)conductor->sex=male; else if(r==2)conductor->sex=female; r=rand()%4+1; if(r==1)conductor->color="white"; else if(r==2)conductor->color="brown"; else if(r==3)conductor->color="black"; else if(r==4)conductor->color="spotted"; r=rand()%34+0; conductor->name=name1[r]; conductor->age=0; r=rand()%100+1; if(r==1||r==2){ conductor->radioactive_mutant_vampire_bunny=true; conductor->sex=vampire; } else{conductor->radioactive_mutant_vampire_bunny=false;} std::cout<<"Bunny "<<conductor->name<<" was born!"<<conductor->sex<<"\n"; conductor->alive=1; conductor->next=new bunny; conductor=conductor->next; conductor->next=0; } } void up_age(){ conductor=root; while(conductor!=NULL){ conductor->age++; conductor=conductor->next; } } void new_bunnies(){ int female_count=0; bool male_count=false; int bunnies_to_create=0; int bunnies_created=0; k=1; conductor=root; while(conductor!=NULL){ if(conductor->sex==female&&conductor->alive==1){female_count=female_count+1; female_color[k]=conductor->color; k++; female_color[k]=conductor->color; k++; } else if(conductor->sex==male){male_count=true;} conductor=conductor->next; } std::cout<<female_count<<" female bunnys."; bunnies_to_create=(female_count*2); std::cout<<bunnies_to_create<<" bunnies should born\n"; if(bunnies_to_create>0){ conductor=root; k=1; while(bunnies_created!=bunnies_to_create){ if(conductor->alive==0){ std::cout<<"creating bunny2\n"; create_bunny(); k++; if(conductor->next!=0){conductor=conductor->next;} } else if(conductor->next==0&&bunnies_created!=bunnies_to_create){ std::cout<<"creating bunny3\n"; create_bunny(); bunnies_created++; k++; conductor->next=new bunny; conductor=conductor->next; conductor->next=0; } else{conductor=conductor->next;} } } conductor=root; } void create_bunny(){ r=rand()%2+1; if(r==1)conductor->sex=male; else if(r==2)conductor->sex=female; conductor->color=female_color[k]; r=rand()%34+0; conductor->name=name1[r]; conductor->age=0; r=rand()%100+1; if(r==1||r==2){ conductor->radioactive_mutant_vampire_bunny=true; conductor->sex=vampire; } else{conductor->radioactive_mutant_vampire_bunny=false;} number_of_bunnies++; std::cout<<"Bunny "<<conductor->name<<" was born!"<<conductor->sex<<"\n"; conductor->alive=1; } void kill_old(){ conductor=root; while(conductor!=NULL){ if(conductor->age>10){ conductor->alive=0; number_of_bunnies--; std::cout<<conductor->name<<" died!";} conductor=conductor->next; } }
Code:size_type max_size() const { // return maximum possible length of sequence size_type _Num = this->_Alval.max_size(); return (_Num <= 1 ? 1 : _Num - 1); }



5Likes
LinkBack URL
About LinkBacks





(edited the code in the first post). Kinda forgot that you only have to seed once, thank you for reminding it. 
