SegFaults

Hello, I have this block of functions
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
using namespace std;

typedef pair<string,string> Pair_allele;

struct Person {
    int id;
    int generation;
    int life;
    bool alive;
    vector<Pair_allele> Genotypes;
    vector<string> Phenotypes;
    vector<string> Gene_List;
    Person(int i, int g) {
        id = i;
        generation = g;
    }
};

void kill (vector<Person> &list, int generation, int limit, string prop) {
    for (int i = 0; i < list.size(); i++) {
        if (list[i].generation == generation - 3) {
            list.erase(list.begin()+i);
        }
        else if (list[i].generation == generation - 2) {
            int random = rand() % 1;
            if (random == 0) {
                list.erase(list.begin()+i);
            }
        }
        else if (list.size() >= limit) {
            if (!i % 2 == 0) {
                list.erase(list.begin()+i);
            }
        }
        else if (list[i].Phenotypes[0] == prop) {
            int random = rand() % 1;
            if (random == 0) {
                list.erase(list.begin()+i);
            }
        }
    }
    for (int i = 0; i < list.size(); i++) {
        list[i].id = i;
    }
}

void Breed( vector<Person> &list, vector<Pair_allele> Phenotypes, vector<Pair_allele> Genotypes, int g, vector<string> genes) {
    int size = list.size();
    for (int i = list.size(); i > size/2; i--) {
        if (list[i].generation == g-1 || list[i].generation == g-2) {
            int person_one = i;
            int person_two = rand() % list.size();
            for (int w = 0; w < list.size(); w++) {
                if (list[person_two].generation != g-1 || list[person_two].generation != g-2) {
                    person_two = rand() % list.size();
                }
                else {
                    break;
                }
            }
            vector<Pair_allele> new_genotypes;
            vector<string> new_phenotypes;
            for (int j = 0; j < genes.size(); j++) {
                //cout << "Here" << endl;
                pair<string,string> Poss_one;
                Poss_one.first = list[person_one].Genotypes[j].first;
                Poss_one.second = list[person_two].Genotypes[j].first;
                pair<string,string> Poss_two;
                Poss_two.first = list[person_one].Genotypes[j].first;
                Poss_two.second = list[person_two].Genotypes[j].second;
                pair<string,string> Poss_three;
                Poss_three.first = list[person_one].Genotypes[j].second;
                Poss_three.second = list[person_two].Genotypes[j].second;
                //cout << "There" << endl;
                int poss = rand() % 3;
                if (poss == 0) {
                    //cout << 1 << endl;
                    new_genotypes.push_back(Pair_allele(Poss_one.first, Poss_one.second));
                }
                else if (poss == 1) {
                    //cout << 2 << endl;
                    new_genotypes.push_back(Pair_allele(Poss_two.first, Poss_two.second));
                }
                else {
                    //cout << 3 << endl;
                    new_genotypes.push_back(Pair_allele(Poss_three.first, Poss_three.second));
                }
                char c = new_genotypes[j].first[0];
                char d = new_genotypes[j].second[0];
                if (isupper(c)|| isupper(d) == true) {
                    new_phenotypes.push_back(Phenotypes[j].first);
                }
                else {
                    new_phenotypes.push_back(Phenotypes[j].second);
                }
            }
            list.push_back(Person(list.size()+1, g));
            for (int q = 0; q < genes.size(); q++) {
                list[list.size()-1].Genotypes.push_back( Pair_allele(new_genotypes[q].first, new_genotypes[q].second) );
                //cout << "Breed: " << new_genotypes[q].first << ":" << new_genotypes[q].second << endl;
                list[list.size()-1].Phenotypes.push_back( new_phenotypes[q] );
            }
        }
    }
    //cout << "Breed" << endl;
}


and when I run it, after a couple (but never fixed) number of loops (The loop increases generation, then calls breed, kill ) it returns a segfault, the debug shoed an error in these lines
1
2
3
4
5
6
7
8
9
                pair<string,string> Poss_one;
                Poss_one.first = list[person_one].Genotypes[j].first;
                Poss_one.second = list[person_two].Genotypes[j].first;
                pair<string,string> Poss_two;
                Poss_two.first = list[person_one].Genotypes[j].first;
                Poss_two.second = list[person_two].Genotypes[j].second;
                pair<string,string> Poss_three;
                Poss_three.first = list[person_one].Genotypes[j].second;
                Poss_three.second = list[person_two].Genotypes[j].second;


Help Please?
Last edited on
Looks overly complicated, what does it do?
The whole program simulates to some extent a species over generations and displays the various stats about there gene makeup
closed account (DSLq5Di1)
1
2
3
    int size = list.size();
    for (int i = list.size(); i > size/2; i--) {
        if (list[i].generation == g-1 || list[i].generation == g-2) {

C++ Arrays are zero-based. The last index of list is the size()-1.
Topic archived. No new replies allowed.