简体   繁体   English

C中带有重复的置换; Valgrind错误

[英]Permutations with Repetitions in C; Valgrind Errors

I'm trying to write a program where essentially I am trying to figure out the combination of a combination lock. 我试图编写一个程序,从本质上讲,我试图弄清楚组合锁的组合。 I take in two inputs from the user, the number of dials to be turned (which I've called max indices) and the highest number each dial can go to (which I've called max number). 我接受了用户的两个输入,即要转动的拨盘数量(我称之为最大索引)和每个拨盘可以转到的最高编号(我称之为最大编号)。 Taking these two inputs, I'm simply trying to brute force my way into the lock. 接受这两个输入,我只是试图强行进入锁中。

So to solve this problem, I tried to take a recursive approach as seen by the function openHelper. 因此,为了解决此问题,我尝试采用一种递归方法,如openHelper函数所见。 Each time a complete combination is created it is tested by a separate function testCombo, and it outputs one of three values. 每次创建完整组合时,都会通过单独的功能testCombo对其进行测试,并输出三个值之一。 It outputs 1 if the combination worked, -2 if the combination failed and you cannot try anymore, and -1 if the combination failed but you can keep guessing. 如果组合有效,则输出1;如果组合失败并且您不能再尝试,则输出-2;如果组合失败,则输出-1,但是您可以继续猜测。

However, when testing this using valgrind, I get this error repeatedly: Process terminating with default action of signal 11 (SIGSEGV). 但是,在使用valgrind进行测试时,反复出现此错误:进程终止于信号11(SIGSEGV)的默认操作。

Is there something I'm doing wrong with creating the arrays or is there just something wrong with the way I've designed the program? 创建数组时我做错了什么,还是我设计程序的方式出了什么问题?

#include <stdio.h>
#include <stdlib.h>

int openHelper(int *input, int *output, int current_index, int max_index,
        int max_number);

int open(max_value, max_indices)
{
    int numbers[max_value];
    int test[max_indices];

    int i; 
    int x;
    /* Create array of all possible numbers */
    for(i = 0; i < max_value; i++){
        numbers[i] = i;
    }
    x = openHelper(numbers, test, 0, (max_indices - 1), max_value);
    return x;
}

int openHelper(int *input, int *output, int current_index, int max_index,
        int max_number)
{
    int i;
    int x;
    for(i = 0; i < max_number; i++){
        output[current_index] = input[i];

        if(current_index == max_index){
            x = testCombo(output);
            if(x != -1){
                return x;
            }
        }
        else{
            openHelper(input, output, (current_index + 1), max_index,
                    max_number);
        }
    }
}

Test combo works by having a counter within the program. 测试组合通过在程序中包含一个计数器来工作。 So if the counter is set to 10 after 10 tries it will return -2. 因此,如果在10次尝试后将计数器设置为10,它将返回-2。 Each time a combination is tried the counter gets decremented. 每次尝试组合时,计数器都会递减。

I don't see anything inherently wrong with your code. 我没有发现您的代码天生就有错误。 It works for me, with a testCombo() function I devised according to your specifications. 它对我testCombo() ,它具有我根据您的规范设计的testCombo()函数。

However, as I remarked in comments, testCombo() does not seem to be presented enough information to do its job correctly. 但是,正如我在评论中所述, testCombo()似乎没有提供足够的信息来正确执行其工作。 Moreover, if the size of the combinations you are testing (ie max_indices ) is smaller than the size testCombo() expects, then it very possibly might read past the end, which could trigger a segfault. 此外,如果您要测试的组合的大小(即max_indices )小于testCombo()预期的大小,则很有可能读到末尾,这可能会引发段错误。

In other words, I'm laying blame on testCombo() , but I can't be any more specific because you have not presented it. 换句话说,我将责任归咎于testCombo() ,但是我不能再具体了,因为您还没有介绍它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM