简体   繁体   English

为什么我会出现段错误?

[英]Why am I getting segfault?

I'm a beginner to C++, and am working on a class that grades a student's answer ("11 3 1133 22322314231432211231 34 2") by comparing it to an answer sheet ("112341423114322314231442314231223422").我是 C++ 的初学者,正在研究 class,它通过将学生的答案(“11 3 1133 22322314231432211231 34 2”)与答题卡(“112341423114322314231442323423212.323423212”)进行比较来给学生的答案打分I must compare each value of the student's answer and if they match, add 2 points;我必须比较学生答案的每个值,如果它们匹配,则加 2 分; if they don't, subtract 1. Now, if the answer is empty, I don't do anything to the student's grade.如果他们不这样做,则减去 1。现在,如果答案为空,我不会对学生的成绩做任何事情。

Right now, the code is as follows:现在,代码如下:

//
#ifndef GRADER_H
#define GRADER_H

using namespace std;

#include <iostream>

class Grader {
  protected:
    int punt;
    string answers, id, stans;
  public:
    Grader(){
      answers = "112341423114322314231442314231223422";
      int i;

      char ans_arr[answers.length()];

      for (i = 1; answers.length(); i++){
        ans_arr[i] = answers[i];
      }
    }

    Grader(string ans) {
      answers = ans;
    }

    void Grade (string ans, string id, string stans) {
      int punt = 0;
      for (int i = 0; ans.length(); i++) {
        if (stans[i] == ans[i]){
          punt = punt + 2;
        } else if ((stans[i] != ans[i]) && (stans[i] != ' ')) {
          punt = punt - 1;
        }
      }

      cout << punt;
    }
};

#endif

//Main

#include <iostream>
#include "grader.h"
using namespace std;

int main() {

  string ans = "112341423114322314231442314231223422";

  Grader a(ans);

  string student_id = "12345";
  string student_ans = "11 3 1133 22322314231432211231 34 2";

  int punt = 0;

  a.Grade(ans,student_id,student_ans);

  return 0;
}

With this, I get a segfault.有了这个,我得到了一个段错误。 I understand I'm dealing with memory I shouldn't deal with, but I have no idea how to make this work.我知道我正在处理 memory 我不应该处理,但我不知道如何进行这项工作。

Your for-loop conditions are not actually conditions.您的 for 循环条件实际上不是条件。

From w3schools :来自w3schools

for (statement 1; statement 2; statement 3) {
  // code block to be executed
}

Statement 1 is executed (one time) before the execution of the code block.

Statement 2 defines the condition for executing the code block.

Statement 3 is executed (every time) after the code block has been executed.

Your conditions are, for example, ans.length() , when they should be i < ans.length() .例如,您的条件是ans.length() ,而它们应该是i < ans.length()

Since ans.length() will always have the same (positive) value, it will be interpreted as the loop needing to continue and i will continue to be incremented.由于ans.length()将始终具有相同的(正)值,因此它将被解释为循环需要继续并且i将继续递增。 Then something like ans[i] is actually attempting to look at memory after the end of the array, resulting in a segfault when that out-of-bounds memory is not allocated to your application.然后像ans[i]这样的东西实际上试图在数组结束后查看 memory,当超出范围的 memory 没有分配给您的应用程序时会导致段错误。

ans.length is 36 and stans.length is 35 . ans.length是 36 和stans.length is 35 The following code reads from the outside of stans .以下代码从stans的外部读取。

for (int i = 0; ans.length(); i++) {
        if (stans[i] == ans[i]){

Here is the memory access error:这是 memory 访问错误:

  Memory access error: reading from the outside of a memory block; abort execution.
  # Reading 1 bytes from 0x9df62c0 will read undefined values.
  #
  # The memory-block-to-be-read (start:0x9df6290, size:48 bytes) is allocated at
  #    unknown_location (report this ::244)
  #
  #  0x9df6290           0x9df62bf
  #  +--------------------------+
  #  | memory-block-to-be-read  |......
  #  +--------------------------+
  #                              ^~~~~~~~~~
  #        the read starts at 0x9df62c0 that is right after the memory block end.
  #
  # Stack trace (most recent call first) of the read.
  # [0]  file:/prog.cc::28, 13
  # [1]  file:/prog.cc::50, 3
  # [2]  [libc-start-main]

You can use this link to debug segfaults of your code in the future.您可以使用此链接在将来调试代码的段错误。 Just click "Start" to build and run your code in the Terminal.只需单击“开始”即可在终端中构建和运行您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM