Segfault初始化一个int

时间:2015-03-16 03:54:03

标签: c++ segmentation-fault

/*Matt Boler
meb0054
hw5.cpp
Compile with gcc in cygwin
*/

#import <iostream>
#import <string>
#import <sstream>
#import <cstdlib>
#import <climits>
#import <assert.h>
using namespace std;

//#define UNIT_TESTING

struct TriviaNode
{
string question;
string answer;
int points;
TriviaNode *next;
};

typedef TriviaNode* NodePtr;

//Input: (1) root is the linked list to be added to
//       (2) Question is the question for the node to ask
//       (3) Answer is the answer to the node's question
//       (4) Points is the point value of the node
//This adds a node to the end of the list
void appendNode(NodePtr& root, string question, string answer, int points);

//Input: (1) root is the linked list to get the length of
//Output: Returns the number of nodes in the linked list
//This calculates the number of nodes in a list
int getListLength(NodePtr& root);

//Input: (1) root is the node to start the list from
//This generates a hardcoded trivia list with 3 predefined questions and answers
void generateHardCodedList(NodePtr& root);

//Input: (1) root is the linked list containing questions to be asked
//       (2) numQuestions is the number of questions to be asked from the list 
//Output: returns o if answered correctly and 1 if answered incorrectly
//This asks the user a question
int askQuestion(NodePtr& root, int numQuestions);


int main()
{
#ifdef UNIT_TESTING
NodePtr head;
cout << "*** This is a debugging version ***" << endl;
cout << "Unit Test Case 1: Ask no questions. The program should give a warning" <<endl;
askQuestion(head, 0);
cout << "Test Case passed..." << endl;

generateHardCodedList(head);
cout << "Unit Test Case 2.1: Ask one question. The tester enters an incorrect answer" << endl;
assert(askQuestion(head, 1) == 1);
cout << "Test Case passed..." << endl;

cout << "Unit Test Case 2.2: Ask one question. The tester enters a correct answer" << endl;
assert(askQuestion(head, 1) == 0);
cout << "Test Case passed..." << endl;

cout << "Unit Test Case 3.1: Ask all questions. The tester enters incorreect answers" << endl;
assert(askQuestion(head, 1) == 1);
assert(askQuestion(head, 2) == 1);
assert(askQuestion(head, 3) == 1);
cout << "Test Case passed..." << endl;

cout << "Unit Test Case 3.2: Ask all questions. The tester enters correect answers" << endl;
assert(askQuestion(head, 1) == 0);
assert(askQuestion(head, 2) == 0);
assert(askQuestion(head, 3) == 0);
cout << "Test Case passed..." << endl;

cout << "Unit Test Case 4: Ask 5 questions in the linked list" << endl;
askQuestion(head, 5);
cout << "*** END OF THE DEBUGGING VERSION ***" << endl;

#else
{
cout << "Welcome to Matt Boler's Trivia Quiz Game!" << endl;
string userContinue = "";
string no = "No";
NodePtr head;
int numQuestions = 3;
generateHardCodedList(head);
while(userContinue.compare(no) != 0)
{
string question, answer;
int score;
cout << "Enter a question:";
getline(cin, question);

cout << "Enter an answer:";
getline(cin, answer);

cout << "Enter award points:";
cin >> score;

cin.clear();
cin.ignore(INT_MAX, '\n');

cout << "Continue? (Yes/No)" << endl;
getline(cin, userContinue);
numQuestions++;
}
//ANYTHING PAST HERE IN THE ELSE BLOCK FAILS VIA SEGFAULT OR JUST NOT RUNNING. NO IDEA WHY
int score = 0;
NodePtr cur = head;
for(int x = 1; x < numQuestions; x++)
{
cur = cur->next;
if(askQuestion(head, x) == 0)
{
  score += cur-> points;
}

}
cout << "Your score is: " << score << endl;
}






#endif
return 0;
}

void appendNode(NodePtr& root, string q, string ans, int pts)
{
NodePtr cur;
NodePtr pre;

cur = new TriviaNode;
assert(cur != NULL);

cur->question = q;
cur->answer = ans;
cur->points = pts;
cur->next = NULL;

if (root == NULL)
root = cur;
else
{
pre = root;
while (pre->next != NULL)
{
  pre = pre->next;
}

pre->next = cur;
}  
}

void generateHardCodedList(NodePtr& root)
{
string q1 = "How long was the shortest war on record? (Hint: how many     minutes)";
string ans1 = "38";
int pts1 = 100;

string q2 = "What was the Bank of America's original name? (Hint: Bank of Italy or Bank of Germany)";
string ans2 = "Bank of Italy";
int pts2 = 50;

string q3 = "What is the best-selling video game of all time? (Hint: Call of Duty or Wii Sports)";
string ans3 = "Wii Sports";
int pts3 = 20;

appendNode(root, q1, ans1, pts1);
appendNode(root, q2, ans2, pts2);
appendNode(root, q3, ans3, pts3);
}

int askQuestion(NodePtr& root, int numQuestions)
{
NodePtr cur;
cur = root;
if(numQuestions > getListLength(root))
{
cout << "Warning: there aren't that many questions in the list" << endl;
}
else if(numQuestions < 1)
{
cout << "Warning: the number of trivia to be asked must be greater than or equal to one" << endl;
}
else
{
for(int i = 1; i < numQuestions; i++)
{
  cur = cur->next;
}
cout << "Question: " << cur->question << endl;
cout << "Player answer: ";
string player_answer;

getline(cin, player_answer);
cin.clear();

if (player_answer == cur->answer)
{
  cout << "Your answer is correct. You recieve " << cur->points << " points." << endl;
  return 0;
}
else
{
  cout << "Your answer is wrong. The correct answer is: " << cur->answer << endl;
}
}
return 1; 
}

int getListLength(NodePtr& root)
{
if(root == NULL)
{
return 0;
}
int count = 0;
NodePtr cur = root;
while(cur != NULL)
{
cur = cur->next;
++count;
}
return count;
}

这是一个将琐事问题添加到c ++中的链接列表的程序。 while循环之后的任何内容都会导致错误。具体来说,初始化int会导致段错误;尝试使用cout打印到控制台时拒绝打印任何内容。

1 个答案:

答案 0 :(得分:2)

在当前版本的代码中,generateHardCodedListroot指针设置为appendNode函数而不进行初始化。调用代码未初始化root(在head中称为main),generateHardCodedList也未初始化它,因此appendNode收到垃圾指针root 1}}。之后所有赌注都已关闭:您的计划已经破产。

我不知道为什么在你的情况下它会在代码中崩溃太多,但无论如何它并不重要。您的generateHardCodedList来电已经中断。

在将head指针初始化为nullptr之前将其传递给generateHardCodedList,或者在nullptr内更好地将其初始化为generateHardCodedList,然后再尝试拨打appendNode {1}}。