为什么我会出现段错误?

Why am I getting segfault?

我是 C++ 的初学者,我正在研究 class 通过将学生的答案(“11 3 1133 22322314231432211231 34 2”)与答案 sheet( “112341423114322314231442314231223422”)。我必须比较学生答案的每个值,如果它们匹配,则加 2 分;如果他们不这样做,减去 1。现在,如果答案为空,我不会对学生的成绩做任何事情。

现在代码如下:

//
#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;
}

有了这个,我得到了一个段错误。我知道我正在处理我不应该处理的内存,但我不知道如何让它工作。

您的 for 循环条件实际上不是条件。

来自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.

例如,您的条件是ans.length(),而它们应该是i < ans.length()

由于 ans.length() 将始终具有相同的(正)值,它将被解释为循环需要继续并且 i 将继续递增。然后 ans[i] 之类的东西实际上是在尝试查看数组末尾后的内存,当超出范围的内存未分配给您的应用程序时会导致段错误。

ans.length 是 36 和 stans.length is 35。以下代码从 stans.

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

这里是内存访问错误:

  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]

您以后可以使用此 link 来调试代码的段错误。只需单击 "Start" 即可在终端中构建和 运行 您的代码。