2015-02-12 55 views
0

我在我正在運行的程序上運行valgrind,並且出現以下錯誤 - 我找不到原因。任何幫助將不勝感激提前。讓我知道如果你需要更多的信息 從標準輸入輸入的問題 - 從valgrind無效讀取/寫入

==27928== Invalid read of size 1 
==27928== at 0x4EB9390: __GI___rawmemchr (rawmemchr.S:25) 
==27928== by 0x4EA514F: _IO_str_init_static_internal (strops.c:45) 
==27928== by 0x4E86D64: __isoc99_vsscanf (isoc99_vsscanf.c:42) 
==27928== by 0x4E86CF7: __isoc99_sscanf (isoc99_sscanf.c:33) 
==27928== by 0x401A76: countAndSort (lab1.c:442) 
==27928== by 0x4014EB: doProcess (lab1.c:316) 
==27928== by 0x4011C2: main (lab1.c:211) 
==27928== Address 0x55bb040 is 0 bytes inside a block of size 10,000,000 free'd 
==27928== at 0x4C27D4E: free (vg_replace_malloc.c:427) 
==27928== by 0x4019D6: countAndSort (lab1.c:430) 
==27928== by 0x4014EB: doProcess (lab1.c:316) 
==27928== by 0x4011C2: main (lab1.c:211) 
==27928== 
==27928== Invalid read of size 1 
==27928== at 0x4C2B834: __GI___rawmemchr (mc_replace_strmem.c:1110) 
==27928== by 0x4EA514F: _IO_str_init_static_internal (strops.c:45) 
==27928== by 0x4E86D64: __isoc99_vsscanf (isoc99_vsscanf.c:42) 
==27928== by 0x4E86CF7: __isoc99_sscanf (isoc99_sscanf.c:33) 
==27928== by 0x401A76: countAndSort (lab1.c:442) 
==27928== by 0x4014EB: doProcess (lab1.c:316) 
==27928== by 0x4011C2: main (lab1.c:211) 
==27928== Address 0x55bb041 is 1 bytes inside a block of size 10,000,000 free'd 
==27928== at 0x4C27D4E: free (vg_replace_malloc.c:427) 
==27928== by 0x4019D6: countAndSort (lab1.c:430) 
==27928== by 0x4014EB: doProcess (lab1.c:316) 
==27928== by 0x4011C2: main (lab1.c:211) 
==27928== 
==27928== Invalid read of size 1 
==27928== at 0x4E7D920: _IO_vfscanf (vfscanf.c:620) 
==27928== by 0x4E86D79: __isoc99_vsscanf (isoc99_vsscanf.c:44) 
==27928== by 0x4E86CF7: __isoc99_sscanf (isoc99_sscanf.c:33) 
==27928== by 0x401A76: countAndSort (lab1.c:442) 
==27928== by 0x4014EB: doProcess (lab1.c:316) 
==27928== by 0x4011C2: main (lab1.c:211) 
==27928== Address 0x55bb040 is 0 bytes inside a block of size 10,000,000 free'd 
==27928== at 0x4C27D4E: free (vg_replace_malloc.c:427) 
==27928== by 0x4019D6: countAndSort (lab1.c:430) 
==27928== by 0x4014EB: doProcess (lab1.c:316) 
==27928== by 0x4011C2: main (lab1.c:211) 
==27928== 
==27928== Invalid read of size 1 
==27928== at 0x4EA36D2: _IO_sputbackc (genops.c:731) 
==27928== by 0x4E7D97B: _IO_vfscanf (vfscanf.c:625) 
==27928== by 0x4E86D79: __isoc99_vsscanf (isoc99_vsscanf.c:44) 
==27928== by 0x4E86CF7: __isoc99_sscanf (isoc99_sscanf.c:33) 
==27928== by 0x401A76: countAndSort (lab1.c:442) 
==27928== by 0x4014EB: doProcess (lab1.c:316) 
==27928== by 0x4011C2: main (lab1.c:211) 
==27928== Address 0x55bb040 is 0 bytes inside a block of size 10,000,000 free'd 
==27928== at 0x4C27D4E: free (vg_replace_malloc.c:427) 
==27928== by 0x4019D6: countAndSort (lab1.c:430) 
==27928== by 0x4014EB: doProcess (lab1.c:316) 
==27928== by 0x4011C2: main (lab1.c:211) 
==27928== 
==27928== Invalid read of size 1 
==27928== at 0x4E7DC7D: _IO_vfscanf (vfscanf.c:1394) 
==27928== by 0x4E86D79: __isoc99_vsscanf (isoc99_vsscanf.c:44) 
==27928== by 0x4E86CF7: __isoc99_sscanf (isoc99_sscanf.c:33) 
==27928== by 0x401A76: countAndSort (lab1.c:442) 
==27928== by 0x4014EB: doProcess (lab1.c:316) 
==27928== by 0x4011C2: main (lab1.c:211) 
==27928== Address 0x55bb040 is 0 bytes inside a block of size 10,000,000 free'd 
==27928== at 0x4C27D4E: free (vg_replace_malloc.c:427) 
==27928== by 0x4019D6: countAndSort (lab1.c:430) 
==27928== by 0x4014EB: doProcess (lab1.c:316) 
==27928== by 0x4011C2: main (lab1.c:211) 
==27928== 
==27928== Invalid read of size 1 
==27928== at 0x4E7E9EF: _IO_vfscanf (vfscanf.c:1781) 
==27928== by 0x4E86D79: __isoc99_vsscanf (isoc99_vsscanf.c:44) 
==27928== by 0x4E86CF7: __isoc99_sscanf (isoc99_sscanf.c:33) 
==27928== by 0x401A76: countAndSort (lab1.c:442) 
==27928== by 0x4014EB: doProcess (lab1.c:316) 
==27928== by 0x4011C2: main (lab1.c:211) 
==27928== Address 0x55bb041 is 1 bytes inside a block of size 10,000,000 free'd 
==27928== at 0x4C27D4E: free (vg_replace_malloc.c:427) 
==27928== by 0x4019D6: countAndSort (lab1.c:430) 
==27928== by 0x4014EB: doProcess (lab1.c:316) 
==27928== by 0x4011C2: main (lab1.c:211) 
==27928== 
==27928== Invalid read of size 1 
==27928== at 0x4EA36D2: _IO_sputbackc (genops.c:731) 
==27928== by 0x4E7DDF3: _IO_vfscanf (vfscanf.c:1811) 
==27928== by 0x4E86D79: __isoc99_vsscanf (isoc99_vsscanf.c:44) 
==27928== by 0x4E86CF7: __isoc99_sscanf (isoc99_sscanf.c:33) 
==27928== by 0x401A76: countAndSort (lab1.c:442) 
==27928== by 0x4014EB: doProcess (lab1.c:316) 
==27928== by 0x4011C2: main (lab1.c:211) 
==27928== Address 0x55bb041 is 1 bytes inside a block of size 10,000,000 free'd 
==27928== at 0x4C27D4E: free (vg_replace_malloc.c:427) 
==27928== by 0x4019D6: countAndSort (lab1.c:430) 
==27928== by 0x4014EB: doProcess (lab1.c:316) 
==27928== by 0x4011C2: main (lab1.c:211) 
==27928== 

不管你怎麼想從每個錯誤的根源,通過大量的試驗和錯誤,註釋掉/後的代碼塊,並分別測試件代碼在測試編譯中,我發現以下是錯誤的來源。如果我在我的程序中註釋此代碼,所有錯誤都會消失。

注意:整數是全局int *類型。 countToUse是一個全局的int類型。這裏的代碼的目標是從標準輸入讀取所有整數,並將它們放入正確大小的動態數組中。

  // Load stdin into a buffer 
      char *buffer = malloc(BUFFER_SIZE); 
      if(buffer == NULL){ 
        fprintf(stderr, "Malloc for stdin buffer in countAndSort() failed. Exiting.\n"); 
        exit(1); 
      } 
      if (fgets(buffer, BUFFER_SIZE, stdin) == NULL) { 
        fprintf(stderr, "Error loading input from stdin into buffer. Exiting.\n"); 
        exit(1); 
      } 

      // Get a count of the numbers to create the array 
      int count = 0; 
      int index = 0; 
      int num, delta; 
      while (index < BUFFER_SIZE && sscanf(&buffer[index], "%d%n", &num, &delta) == 1){ 
        count++; 
        index += delta; 
      } 

      // Initialize the array with the proper size 
      integers = malloc(count*sizeof(int)); 
      if(integers == NULL){ 
        fprintf(stderr, "Malloc for integer array in countAndSort() failed. Exiting. \n"); 
        exit(1); 
      } 

      // Load the integers into the array 
      index = 0; 
      for (int i = 0; i < count; i++){ 
// LINE 442 IS RIGHT BELOW THIS LINE ------------------------------------ 
        if (index < BUFFER_SIZE && sscanf(&buffer[index], "%d%n", &integers[i], &delta) != 1){ 
          fprintf(stderr, "There was a problem reading the buffer!\n"); 
          exit(1); 
        } 
        index += delta; 
      } 

      if(buffer != NULL) 
        free(buffer); 

      countToUse = count; 
+0

'doProcess()' – Cyclonecode 2015-02-12 01:09:18

+0

@Cyclone它不是由doProcess引起的。我已經排除了。 doProcess(在第323行)調用包含此代碼的函數(countAndSort)。 – JayB 2015-02-12 01:09:49

+0

@Cyclone在計數進程中的第323行讀取「countAndSort()」 – JayB 2015-02-12 01:11:04

回答

1

你需要確保你不讀超出buffer界限,這樣

while ((index < 10000000) && (sscanf(&buffer[index], "%d%n", &num, &delta) == 1)) 

這同樣適用於所有的buffer[index]的。

此外,從錯誤似乎你有free() d buffer然後試圖從它讀取。

+0

我已經做了這些修改,同樣的錯誤仍然存​​在 – JayB 2015-02-12 01:27:02