【问题标题】:Segfault with initializing an int初始化 int 的段错误
【发布时间】:2015-05-18 03:05:38
【问题描述】:
/*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 打印到控制台拒绝打印任何内容。

【问题讨论】:

  • 欢迎来到 StackOverflow。如果您不给我们minimal complete example,我们将无法为您提供帮助。
  • 我们想要更少的代码,而不是更多的代码。
  • 什么是#import &lt;iostream&gt;??? #import?
  • 你忘了问一个具体的、可以回答的问题。您的问题是“如何调试程序?”

标签: c++ segmentation-fault


【解决方案1】:

在当前版本的代码中,您的generateHardCodedListroot 指针指向appendNode 函数而不对其进行初始化。调用代码没有初始化root(在main中调用head),generateHardCodedList也没有初始化它,所以appendNode接收到一个垃圾指针root。在那之后,所有的赌注都没有了:你的程序已经坏了。

我不知道为什么在你的情况下它在代码的后面崩溃了这么多,但这并不重要。您的 generateHardCodedList 呼叫已中断。

在将head 指针传递给generateHardCodedList 之前使用nullptr 初始化您的指针,或者在尝试调用appendNode 之前更好地将其初始化为generateHardCodedList 内的nullptr

【讨论】:

  • 哇。好吧,我完全错过了。谢谢!
猜你喜欢
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 2016-06-03
  • 2021-09-25
  • 2011-10-29
  • 2014-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多