1

我正在努力解决一个编程实践问题,并且要回到 C 来重新学习。无论如何,我通过 calloc 获得的数组存在错误。我要回来的数组没有被初始化为零。这是我的代码的摘录,以及它产生的输出。我担心我可能只是错过了一些令人痛苦的明显或特殊的东西。

int * check_digits;
    // For loop iterating over all numbers with fangsize digits
    for (int i = smallest_fang; i < largest_fang; i++) {

        char fang1_word[N];
        sprintf(fang1_word, "%d", i);

        for (int j = i; j < largest_fang; j++) {

            int dracula;
            char dracula_word[N];
            char fang2_word[N];

            check_digits = (int *) calloc((size_t) N, (size_t) sizeof(int));
            printf("HELLO\n");
            for (int arg = 0; arg < 10; arg++) {
                printf("%d\n", check_digits[arg]);
            }
            printf("\n");

            // Verify they both aren't divisible by ten.
            if (((i % 10) == 0) && ((j % 10) == 0)) { 
                free(check_digits);
                continue;   
            }


            // Calculate potential vampire
            dracula = i * j;
            printf("Here, I is %d and J is %d.\n", i, j);
            printf("The potential vampire is %d\n", dracula);

            // Verify potential vampire has enough digits
            sprintf(dracula_word, "%d", dracula);
            sprintf(fang2_word, "%d", j);

            printf("Lenth of dracula word is %ld\n", strlen(dracula_word));
            if ((int) strlen(dracula_word) != N) {
                printf("Wasn't long enough.\n");
                free(check_digits);
                continue;
            }


            // Count up all the vampire's digits into check_digits
            for (int k = 0; k < N; k++) {
                char digit_char;
                int digit;

                digit_char = dracula_word[k];
                digit = (int) strtol( (const char *) &digit_char, (char**) NULL, 10);
                printf("digit is %d\n", digit);
                check_digits[digit]++;
            }

            // Print out check digits
            printf("\nPrinting out check digits.\n");
            for (int k = 0; k < 10; k++) {
                printf("The digit %d occurs %d times.\n", k, check_digits[k]);
            }

            // See if they all match. No need to make sure any value of
            // check_digits is above zero because digits must already add
            // up.
            int failed = 0;
            for (int k = 0; k < N/2; k++) {
                char digit_char;
                int digit;

                digit_char = fang1_word[k];
                digit = (int) strtol( (const char *) &digit_char, (char**) NULL, 10);

                check_digits[digit]--;
                if (check_digits[digit] < 0) {
                    failed = 1;
                    break;
                }

                digit_char = fang2_word[k];
                digit = (int) strtol( (const char *) &digit_char, (char**) NULL, 10);

                check_digits[digit]--;
                if (check_digits[digit] < 0) {
                    failed = 1;
                    break;
                }
            }

            // Failed at some point during digit check phase
            if (failed) { 
                free(check_digits);
                continue;   
            }

            // They all seem to match. Print vampire number
            else {
                printf("Found vampire number: %d * %d = %d\n", i, j, dracula);
            }
        }

当我针对已知的误报运行代码和 grep 时,我得到以下几行:

240211-HELLO
240217-0
240219-0
240221-0
240223-0
240225-3
240227-5
240229-1
240231-3
240233-0
240235-2
240237-
240238-Here, I is 21 and J is 58.
240265:The potential vampire is 1218
240295-Lenth of dracula word is 4
240322-digit is 1
240333-digit is 2
240344-digit is 1
240355-digit is 8
240366-
240367-Printing out check digits.
240394-The digit 0 occurs 0 times.
240422-The digit 1 occurs 2 times.
240450-The digit 2 occurs 1 times.
240478-The digit 3 occurs 0 times.
240506-The digit 4 occurs 3 times.
240534-The digit 5 occurs 5 times.
240562-The digit 6 occurs 1 times.
240590-The digit 7 occurs 3 times.
240618-The digit 8 occurs 1 times.
240646-The digit 9 occurs 2 times.
240674:Found vampire number: 21 * 58 = 1218

本质上,在 calloc 返回内存缓冲区之后,我将其打印出来,并看到它并不清楚,导致以后出现问题。

4

1 回答 1

2

我认为错误在这里:

check_digits = (int *) calloc((size_t) N, (size_t) sizeof(int));
printf("HELLO\n");
for (int arg = 0; arg < 10; arg++) {
    printf("%d\n", check_digits[arg]);
}

您分配了一个包含 N 个元素(可能是 4 个)的数组,但随后您在循环中访问了10 个元素。事实上,前 4 个元素正确设置为零,因此calloc正常工作。其他值(从 4 到 9)是随机值,因为它们超出了使用calloc分配的内存,因此它们未设置为零。

要解决该问题,您应该在访问check_degitis元素的每个循环中使用N。

于 2013-07-23T05:57:02.237 回答