Trouble with linked lists.(Graduation excercise)

Hi, I'm trying to do a beginner excercise called "graduation". And I'm getting
Unhandled exception at 0x774a15de (ntdll.dll) in Graduation.exe: 0xC0000005: Access violation writing location 0x00000000.


I 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?

Here is my code(void new_bunnies seems to be causing the problem). The exception doesn't occure at every excecution.:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#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()
{
	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++;

		srand(time(NULL)+n);
		r=rand()%2+1;
		if(r==1)conductor->sex=male;
		else if(r==2)conductor->sex=female;

		srand(time(NULL)+n);
		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";

		srand(time(NULL)+n);
		r=rand()%34+0;
		conductor->name=name1[r];

		conductor->age=0;

		srand(time(NULL)+n);
		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(){
	srand(time(NULL)+k);
		r=rand()%2+1;
		if(r==1)conductor->sex=male;
		else if(r==2)conductor->sex=female;

		conductor->color=female_color[k];

		srand(time(NULL)+k);
		r=rand()%34+0;
		conductor->name=name1[r];

		conductor->age=0;

		srand(time(NULL)+k);
		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;
	
	}
}

Here is the snippet of code where visual studio points me after the exception.
1
2
3
4
5
size_type max_size() const
		{	// return maximum possible length of sequence
		size_type _Num = this->_Alval.max_size();
		return (_Num <= 1 ? 1 : _Num - 1);
		}
Last edited on
Did you set the last link of conductor to NULL ? inf function create_5_bunnies
conductor->next=0;
Line 120 I guess i did... is that important?
I found out that the problem was about array female_count size, set it to 1000 and it's okay now.
Topic archived. No new replies allowed.