私はプログラミングの実践問題の解決に取り組んでおり、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 がメモリ バッファを返した直後にそれを出力すると、それが明確でないことがわかり、後で問題が発生します。