如何在我的C++ PythGravaTrim取取器中修复“访问违规读取位置”错误?

如何在我的C++ PythGravaTrim取取器中修复“访问违规读取位置”错误?,c++,pythagorean,C++,Pythagorean,我现在正在做一个毕达哥拉斯三重查找器。我已经能够使它工作,但现在我正在清理程序返回的结果,以避免重复。该程序将找到一个类似于3,4,5的毕达格三元组,但随后将再次找到与4,3,5相同的三元组。为了解决这个问题,我添加了一些代码,将毕达格三元组存储在3个数组中:storeA、storeB和storeC。然后我有一个函数遍历先前存储的变量,并将每个变量与当前值进行比较。为了访问比较函数中的数组,我必须将数组设置为全局数组。当我启动程序时,它工作正常,但当我输入一个值(如10)时,它会失败,错误访问冲

我现在正在做一个毕达哥拉斯三重查找器。我已经能够使它工作,但现在我正在清理程序返回的结果,以避免重复。该程序将找到一个类似于3,4,5的毕达格三元组,但随后将再次找到与4,3,5相同的三元组。为了解决这个问题,我添加了一些代码,将毕达格三元组存储在3个数组中:storeA、storeB和storeC。然后我有一个函数遍历先前存储的变量,并将每个变量与当前值进行比较。为了访问比较函数中的数组,我必须将数组设置为全局数组。当我启动程序时,它工作正常,但当我输入一个值(如10)时,它会失败,错误访问冲突读取位置为0x011F5000。如果有人能告诉我我的错误,我将非常感激

repetitionCheck是我用来比较值的函数

#include "stdafx.h"
#include <iostream>

using namespace std;

int squared(int input);
bool pythagCheck(int a, int b, int c);
bool repetitionCheck(int a, int b, int c);

int x;
int increment;
int storeA[1000];
int storeB[1000];
int storeC[1000];

int main()
{
    int a = 0, b = 0, c = 0;

    //input
    cout << "Specify Boundary: ";
    cin >> x;
    cout << "\n\n";

    //function
    for (a=0; a<=x; a++) 
    {
        if (pythagCheck(a, b, c) && repetitionCheck(a, b, c))
        {
            increment++;
            storeA[increment] = a;
            storeB[increment] = b;
            storeC[increment] = c;
        }

        for (b=0; b<=x; b++) 
        {
            if (pythagCheck(a, b, c) && repetitionCheck(a, b, c))
            {
                increment++;
                storeA[increment] = a;
                storeB[increment] = b;
                storeC[increment] = c;
            }

            for (c=0; c<=x; c++) 
            {
                if (pythagCheck(a, b, c) && repetitionCheck(a, b, c))
                {
                    increment++;
                    storeA[increment] = a;
                    storeB[increment] = b;
                    storeC[increment] = c;
                }
            }
        }
    }

    system("pause");

    return 0;
}

int squared(int input)
{
    return input * input;
}

bool pythagCheck(int a, int b, int c) 
{
    if (squared(a) + squared(b) == squared(c) && a != 0 && b != 0 && c != 0)
    {
            cout << "a = " << a << ", b = " << b << ", c = " << c << endl;
            return true;
    }
}

bool repetitionCheck(int a, int b, int c) 
{
    for (int i = 0; i >= increment; i++) 
    {
        if (storeA[i] == b) { return false; }
    }

    for (int i = 0; i >= increment; i++) 
    {
        if (storeC[i] == c) { return false; }
    }

    return true;
}

问题出在函数中

bool repetitionCheck(int a, int b, int c) 
{
    for (int i = 0; i >= increment; i++) 
    {
        if (storeA[i] == b) { return false; }
    }
    for (int i = 0; i >= increment; i++) 
    {
        if (storeC[i] == c) { return false; }
    }
    return true;
}
全局变量增量没有显式初始化。所有变量都应该初始化

但由于它是一个globale变量,编译器会将其设置为默认值,即0

然后,当您调用repetitionChecka,b,c函数时,for循环将几乎永远运行,因为i总是>=increment。然后访问数组索引>1000,即数组大小

然后你会出现seg故障


也许你的意思是你为x输入了什么值?生成x+1^3个元素-很快就会超过1000个。请注意,repetitionCheck始终返回true-它的任何循环都不会运行。这将是学习使用调试器逐步检查代码以查看其行为与预期不符的最佳时机。另外,这也是一个很好的阅读方法。避免重复的一个非常简单的方法是在测试之前对值进行排序,或者确保生成的任何测试都是有序的,也就是说,当第一个数字是1时,无需从0开始第二个数字在重复检查中查看for循环。他们将执行多少次?@1201程序感谢您的帮助!我纠正了那个愚蠢的错误。