Bus error and Segmentation fault help

Hello I am trying to code an assignment in C++ in the nachos operating system, and i everything was going good until I started getting "Bus error" and "Segmentation Fault" At first it was just seg faults and with google i was able to get rid of them (keep in mind it has always compiled correctly, this is all runtime), but then all the sudden it said bus error and now says seg fault even when i go back a few steps. Here is my current code that gets the bus error, sorry for length due to nachos being finicky I gave up on splitting it up into different files: (The problem doesn't occur when i comment out lines 133, 137, 141, and 145, or the pokemon->Append(&players[count]); etc.)


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
// threadtest.cc 
//	Simple test case for the threads assignment.
//
//	Create two threads, and have them context switch
//	back and forth between themselves by calling Thread::Yield, 
//	to illustratethe inner workings of the thread system.
//
// Copyright (c) 1992-1993 The Regents of the University of California.
// All rights reserved.  See copyright.h for copyright notice and limitation 
// of liability and disclaimer of warranty provisions.

#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#include "math.h"
#include "copyright.h"
#include "system.h"
#include "list.h"
//#include "player.h"
//----------------------------------------------------------------------
// SimpleThread
// 	Loop 5 times, yielding the CPU to another ready thread 
//	each iteration.
//
//	"which" is simply a number identifying the thread, for debugging
//	purposes.
//----------------------------------------------------------------------

#ifndef PLAYER_H
#define PLAYER_H

#include "copyright.h"

class Player{
  
  int skill;
  char favGame;
  
public:
  ~Player(void);
  Player(void);
  void setGame(char);
  char getGame(void);
  void setSkill(int);
  int getSkill(void);
  void printSkill(void);
};

#endif


Player::Player(void){
  favGame = 'a';
  skill = 1;
  //printf("skill = %d \n", &skill);
}

Player::~Player(void){
  //printf("hello again \n");
}

void Player::setGame(char a){
  favGame = a;
}

char Player::getGame(void){
  return favGame;
}

void Player::setSkill(int i){
  skill = i;

}
int Player::getSkill(void){
  return skill;
}

void Player::printSkill(void){
  printf("skill = %d \n", skill);
}




void
SimpleThread(int which)
{
    int num;
    
    for (num = 0; num < 5; num++) {
	printf("*** thread %d looped %d times\n", which, num);
        currentThread->Yield();
    }
}

//----------------------------------------------------------------------
// ThreadTest
// 	Set up a ping-pong between two threads, by forking a thread 
//	to call SimpleThread, and then calling SimpleThread ourselves.
//----------------------------------------------------------------------

void
ThreadTest()
{
    DEBUG('t', "Entering SimpleTest");

    Thread *t = new Thread("forked thread");
    printf("Hi \n");
    int count = 1;
    int a;
    char c;

    List *pokemon;
    List *finalFantasy;
    List *pacMan;
    List *legendZelda;
    Player *players[15];
    for(count = 0; count < 15; count ++){
      players[count] = new Player();
    }

    
    //random seed
    srand( time(NULL));
    //for loop which assigns a skill and favorite game to all 15 players 
    for( count = 0; count < 15; count++){
      players[count]->setSkill( rand() %10+1);
      a = rand() %4+1;
      switch(a){
      case 1:
	c = 'a';
	pokemon->Append(&players[count]);
	break;
      case 2:
	c = 'b';
	finalFantasy->Append(&players[count]);
	break;
      case 3:
	c = 'c';
	pacMan->Append(&players[count]);
	break;
      case 4:
	c = 'd';
	legendZelda->Append(&players[count]);
	break;
      default:
	break;
      }
      players[count]->setGame(c);
    }

    
    printf("hello \n");
    pokemon = new List;
    finalFantasy = new List;
    pacMan = new List;
    legendZelda = new List;

    for(count = 0; count < 15; count++){
      printf("Player %d ticket = %c%d \n", count, players[count]->getGame(), players[count]->getSkill());
      
   }
    
     
    t->Fork(SimpleThread, 1);
    SimpleThread(0);
}


also here is the code for the list Append method I'm trying to call, which seems to be causing the trouble:
1
2
3
4
5
6
7
8
9
10
11
12
13
void
List::Append(void *item)
{
    ListElement *element = new ListElement(item, 0);

    if (IsEmpty()) {		// list is empty
	first = element;
	last = element;
    } else {			// else put it after last
	last->next = element;
	last = element;
    }
}
On line 132 you are using 'pokemon' and on line 154 you're creating the object. That must go wrong.
haha *facepalm this is definitely top ten oversights in my programming history. thanks a lot!
Topic archived. No new replies allowed.