[英]Using calloc in C to initialize int array, but not receiving zeroed out buffer
我正在努力解決編程實踐問題,並且正准備回到C進行解決。 無論如何,我在通過calloc獲得的數組中存在bug。 我要返回的數組未初始化為零。 這是我的代碼的摘錄,以及它產生的輸出。 恐怕我可能只是想念一些明顯或特質的東西。
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返回內存緩沖區后,立即將其打印出來,然后看不清楚,這會在以后導致問題。
我認為錯誤在這里:
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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.