【问题标题】:C++ Segmentation fault(core dumped) and using structsC++ 分段错误(核心转储)和使用结构
【发布时间】:2017-06-12 17:38:07
【问题描述】:

我有一个任务是编写一个 maxSubArray 算法。它大部分都在工作,但我在返回结构时遇到了一些问题。

以下是相关文件(对于文字墙感到抱歉,但我不确定如何指出此错误):

main.cpp

#include <iostream>

#include "./MaxSubarray.h"

using namespace std;

#define TEST(test) { \
  testNum++; \
  if (!(test)) { \
    cerr << "Test " << testNum << " failed" << endl; \
    numFails++; \
  } \
}

int runTests() {
  int numFails = 0;
  int testNum = 0;

   {
     //          0   1   2*  3  4   5
     int A[] = { 1, -4, 14, -2, 3, -1 };
     Result r = findMaxCrossingSubarray(A, 0, 2, 5);
     Result c(2, 4, 15);
     TEST(r == c);

   }

   {
     //          0  1   2  3*  4   5  6
     int A[] = { 0, 5, -4, 1, -2, -3, 6 };
     Result r = findMaxCrossingSubarray(A, 0, 3, 6);
     Result c(1, 6, 3);
     TEST(r == c);
   }

   {
     //          0  1   2  3*  4   5  6
     int A[] = { 0, 5, -4, 1, -2, -3, 5 };
     Result r = findMaxCrossingSubarray(A, 0, 3, 6);
     Result c(1, 6, 2);
     TEST(r == c);
   }

   {
     int A[] = { 13, -3, 4 };
     Result r = findMaxSubarray(A, 0, 2);
     Result c(0, 2, 14);
     TEST(r == c);
   }

   {
     int A[] = { 13, 4, -3 };
     Result r = findMaxSubarray(A, 0, 2);
     Result c(0, 1, 17);
     TEST(r == c);
   }

   {
     int A[] = { -3, 4, 13 };
     Result r = findMaxSubarray(A, 0, 2);
     Result c(1, 2, 17);
     TEST(r == c);
   }

   {
     int A[] = { 4, -3, 13 };
     Result r = findMaxSubarray(A, 0, 2);
     Result c(0, 2, 14);
     TEST(r == c);
   }

   {
     int A[] = { 4, -3, -13, 5, 3 };
     Result r = findMaxSubarray(A, 0, 4);
     Result c(3, 4, 8);
     TEST(r == c);
   }

   {
     int A[] = { 4, 3, -13, -5, 3 };
     Result r = findMaxSubarray(A, 0, 4);
     Result c(0, 1, 7);
     TEST(r == c);
   }

   {
     int A[] = { -4, 4, -3, 5, -3 };
     Result r = findMaxSubarray(A, 0, 4);
     Result c(1, 3, 6);
     TEST(r == c);
   }

   {
     int A[] = { 13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7 };
     Result r = findMaxSubarray(A, 0, 15);
     Result c(7, 10, 43);
     TEST(r == c);
   }


  const int numSuccesses = testNum - numFails;
  cout << numSuccesses << "/" << testNum << " tests succeeded" << endl;

  return numFails;
}

int main() {
  // TODO: Add test code as necessary.
  // This file will NOT be submitted, though!

  return runTests();
}

MaxSubarray.cpp

#include "./MaxSubarray.h"
#include <iostream>
// Provides floor, ceil, etc.
#include <cmath>
#include <climits>
using namespace std;

//Kalen Williams
//27 January 2017

Result findMaxCrossingSubarray(int* array, int low, int mid, int high){
    int leftSum = INT_MIN;
    int sum = 0;
    int maxLeftIndex;

    for(int i = mid; i >= low; i--){
        sum = sum + array[i];

        if(sum > leftSum){
            leftSum = sum;
            maxLeftIndex = i;
        }
    }

    int rightSum = INT_MIN;
    sum = 0;
    int maxRightIndex;

    for(int j = mid + 1; j <= high; j++){
        sum = sum + array[j];

        if(sum > rightSum){
            rightSum = sum;
            maxRightIndex = j;
        }
    }

    int totalSum = leftSum + rightSum;
    return Result(maxLeftIndex, maxRightIndex, totalSum);

}

Result findMaxSubarray(int* array, int low, int high){
    if(high = low){
        return Result(low, high, array[low]);
    }
    else{
        int mid = (low + high) / 2;
//
        Result leftArray = findMaxSubarray(array, low, mid);
        Result rightArray = findMaxSubarray(array, mid + 1, high);
        Result crossArray = findMaxCrossingSubarray(array, low, mid, high);

        if(leftArray.sum >= rightArray.sum && leftArray.sum >= crossArray.sum){
            return leftArray;
        }
        else if(rightArray.sum >= leftArray.sum && rightArray.sum >= crossArray.sum){
            return rightArray;
        }
        else{
            return crossArray;
        }
//

    }

}

在代码按原样运行的情况下,我通过了前 3 个测试,因为我的 findMaxCrossingSubarray 工作,但是,当我取消注释 findMaxSubArray 中的代码时,我收到一个错误

分段错误(核心转储)

我已经对此问题进行了大量研究,并且知道这意味着我正在尝试引用尚未为程序分配的内存,我只是不确定如何缩小问题范围。我尝试使用 -Wall 进行编译,但这给了我一堆似乎与此无关的错误。

【问题讨论】:

  • 在调试器下运行程序,看看报错时你的变量值是多少。
  • 不要注释掉您遇到问题的代码部分。 SO 的突出显示使代码更难阅读。
  • 即使您认为来自-Wall 的警告不相关,您仍然应该修复它们。你知道的不够多,无法确定。
  • @Barmar 好的,已修复,我现在将进行调试。问题是我只是使用 vim 并给出了 make 文件,所以据我所知,我必须导入 IDE 来检查它
  • @Bamar 原来我有这么多不相关错误的原因是我使用 gcc 而不是 g++ 编译。使用 g++ 后,所有错误都是对 findMaxCorssingSubarray 的未定义引用和对 findMaxSubarray 的未定义引用,现在看来肯定是相关的

标签: c++ struct segmentation-fault


【解决方案1】:

没有看到头文件我不能确定这是Seg错误的原因,但是你在findMaxSubarray的第一行有一个bug:

if (high = low) {

你显然是指high == low。你应该得到一些编译器警告。如果您是“const nazi”,编译器会发现这一点...(即错误而不是警告):我的意思是,当然,将const int highconst int low 放在函数定义中(const在声明中被忽略,顺便说一句)。

【讨论】:

  • 如果您愿意,我可以上传头文件,但可以假设这是问题所在,因为一旦纠正,它就通过了所有测试。
猜你喜欢
  • 1970-01-01
  • 2022-01-14
  • 2017-02-25
  • 2016-07-12
  • 2018-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多