Segmentation fault with previous on a linked list?

I am wondering why I am getting a seg fault within this loop. It crashes on the last for loop when it tries to through previous nodes. Does anyone have any thoughts as to why this happens? The Person class is after the while loop if it helps. Let me know if you need any other pieces of code to figure it out.

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
 Person *temp_new = root;
 while(temp_person != NULL)
    {
    srand(time(NULL));
    int num1 = temp_person->island;
    char char1 = temp_person->status;
    temp_person = temp_person->next;
    for(int i=0; i<contact_rate; i++)
    {
        int num2 = temp_person->island;
        char char2 = temp_person->status;
        if(num1 == num2)
        {
            if(char1 == 'h' && char2 == 'n')
            {
                int random = (rand()%100)+1;
                cout << random << endl;
                if(random<transmission_probability)
                {
                    for(int j=0; j<i+1; j++)
                    {
                        temp_person = temp_person->previous;
                    }
                    temp_person->setStatus(infected_status);
                    cout << "infected\n";
                    temp_person = temp_person->next;
                }
            }
            else if(char1 == 'n' && char2 == 'h')
            {
                int random = (rand()%100)+1;
                cout << random << endl;
                if(random<transmission_probability)
                {
                    temp_person->setStatus(infected_status);
                    temp_person = temp_person->next;
                }
            }
            else
            {
                temp_person = temp_person->next;
            }
        }
    }
    for(int k=0; k<contact_rate; k++)
    {
        temp_person = temp_person->previous;
    }
}

class Person
{
friend class Island;

private:
    char status;
    int island;
    int days_infected;
    int days_infectious;
public:
    Person *next;
    Person *previous;
    Person();
    Person(char&, int&);
    char setStatus(char&);
    int infected();
    int infectious();
};

Person::Person()
{
}

Person::Person(char& s, int& i)
{
    status = s;
    island = i;
    next = NULL;
}

char Person::setStatus(char& s)
{
    status = s;
}

int Person::infected()
{
    days_infected++;
}

int Person::infectious()
{
    days_infectious++;
}
I don't know why you're getting a segfault, but you do have a problem at line 4.

Do not call srand() multiple times (in a loop). srand() sets the RNG to a particular starting point. Calling srand() repeatedly can cause the RNG to return the same random numbers. srand() should be called ONCE at the beginning of main().
http://www.cplusplus.com/reference/cstdlib/srand/

Apparently my previous pointer was not set to anything. I'm trying to figure out what I point it to so that way it goes in the reverse order of the linked list instead of the next node. Anyone have any suggestions?
Apparently my previous pointer was not set to anything.

The only place in your code where the values are set is here:
1
2
3
4
5
6
7
Person::Person(char& s, int& i)
{
    status = s;
    island = i;
    next = NULL;
    previous = NULL;
}


By the way, nowadays, you should be using nullptr rather than NULL here.

In the other parts of the code where you are constructing the list, then you should be in a position to set the next and previous pointers to the required values so the list functions properly in both forward and reverse.

Just reading through the code, you have a lot of functions which promise to return a value but do not in fact do so, for example:
1
2
3
4
char Person::setStatus(char& s)
{
    status = s;
}


These may seem innocuous enough, but this omission (of the return value;) can cause program crashes, so should be fixed. The appropriate solution here would be to change the type to void:
1
2
3
4
void Person::setStatus(char& s)
{
    status = s;
}
Last edited on
Registered users can post here. Sign in or register to post.