kalenpw
kalenpw

Reputation: 695

C++ Segmentation fault(core dumped) and using structs

I have an assignment to write a maxSubArray algorithm. It is mostly working, but I am having some issues with returning structs.

Here are the relevant files (sorry for the wall of text, but I'm not sure how to pin point this error):

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

    }

}

with the code ran as is, I pass the first 3 tests because my findMaxCrossingSubarrayworks, however, when I uncomment the code in findMaxSubArray I get an error

Segmentation fault (core dumped)

I've done quite a bit of research on this issue and know it means I am trying to reference memory that hasn't been allocated for the program, I am just unsure how to narrow down the issue. I tried compiling with -Wall, but that gave me a bunch of various errors none of which seemed relevant to this.

Upvotes: 0

Views: 479

Answers (1)

James Hirschorn
James Hirschorn

Reputation: 7994

I can't be sure that this is the cause of the Seg fault without seeing the header file, but you have a bug in the first line of findMaxSubarray:

if (high = low) {

You obviously meant high == low. You should have got some compiler warning. If you were a "const nazi" the compiler would have caught this...(i.e. and error instead of a warning): I mean, of course, putting const int high and const int low in the function definitions (the const is ignored in the declaration, btw).

Upvotes: 2

Related Questions