2013-07-23 43 views
1

我正在解決一個編程實踐問題,並且要回到C來踢球。無論如何,我有一個數組,我通過calloc獲取錯誤。我回來的數組沒有被初始化爲零。下面是我的代碼的摘錄,以及它生成的輸出。恐怕我可能會錯過某些顯而易見或特殊的東西。在C中使用calloc來初始化int數組,但沒有收到零化緩衝區

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可用於返回一個內存緩衝區之後,我把它打印出來,看看這還不清楚,以後會導致問題。

+1

什麼是N = ??? – Jeyaram

+0

大概愚蠢的問題是'N == 4'嗎? – Casey

+0

是的N是4抱歉。 – Tatterdemalion

回答

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)的數組,但你訪問元素在週期。如果事實上,前4個元素被正確設置爲零,所以calloc正確工作。其他值(從4到9)是隨機值,因爲它們超出了使用calloc分配的內存,因此它們不會設置爲零。

爲了解決這個問題,在您訪問檢查 _你應該在每個週期中使用ňdegitis元素。