[英]C++ Segmentation fault(core dumped) and using structs
我有一个任务来编写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进行编译,但这给了我很多错误,这些错误似乎都与此无关。
我无法确定这是导致Seg错误的原因,而没有看到头文件,但是findMaxSubarray
的第一行中存在一个错误:
if (high = low) {
您显然意味着high == low
。 您应该收到一些编译器警告。 如果您是“ const nazi”,则编译器会捕获到此...(即错误而不是警告):我的意思是,当然是在函数定义中将const
const int high
和const int low
(忽略const
)在声明中,顺便说一句)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.