2013-10-18 22 views
0

我想寫一個函數插入到字典(字符串數組),但按字母順序(詞法)順序,但我運行到一個無效的大小讀取8錯誤,我不完全確定爲什麼。插入爲了一個字符串數組

這裏是我的代碼:

int insertWord(char **array, int *count, char word[]) 
{ 
    char *wordPtr; 

    wordPtr = (char *)malloc((strlen(word) + 1) * sizeof(char)); 
    if (wordPtr == NULL) 
    { 
     fprintf(stderr," Malloc of array[%d] failed!\n", *count); 
     return -1; 
    } 
    /* Memory for this word has been allocated, so copy characters 
     and insert into array */ 

    strcpy(wordPtr, word); 

    // Iterate through the word array 
    // Check if str1 > other strings 
    // Lower ascii value = earlier in the alphabet 
    // Will return neg value if str1 < str2 (str1 comes before str2) 
    // Will return 0 if they are equal 
    // Will return pos value if str1 > str2 (str1 comes after str2) 
    // Check for an element that comes after the given word in the alphabet 
    bool greaterElementFound = false; 
    int indexLoc = *count; 
    for(int i = 0 ; i < *count ; i ++){ 
      // If compare is a neg #, that means that wordPtr comes before array[i] 
      // So array[i] must be shifted right, and wordPtr must be inserted in its place 
      if(strcasecmp(wordPtr, array[i]) < 0){ 
        greaterElementFound = true; 
        indexLoc = i; 
        break; 
      } 
    } 
    if(greaterElementFound == true){ 
      // Account for overwrite of last element 
      array[*count+1] = array[*count]; 
      // Shift all elements over from indexLoc to *count 
      for(int i = *count; i > indexLoc; i--){ 
        array[i] = array[i-1]; 
      } 
    } 
    array[indexLoc] = wordPtr; 

    (*count)++; 

return 0; 
} 

我得到抑制的錯誤的valgrind:

==4123== ERROR SUMMARY: 2 errors from 1 contexts (suppressed: 4 from 4) 
==4123== 
==4123== 2 errors in context 1 of 1: 
==4123== Invalid write of size 8 
==4123== at 0x401056: insertWord (in /import/linux/home/jball2/CLab/lab2) 
==4123== by 0x400E3E: loadArray (in /import/linux/home/jball2/CLab/lab2) 
==4123== by 0x400AAE: main (in /import/linux/home/jball2/CLab/lab2) 
==4123== Address 0x51b1450 is 0 bytes after a block of size 400 alloc'd 
==4123== at 0x4C2779D: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) 
==4123== by 0x400D91: loadArray (in /import/linux/home/jball2/CLab/lab2) 
==4123== by 0x400AAE: main (in /import/linux/home/jball2/CLab/lab2) 

如果任何人都可以點我在將不勝感激正確的方向,謝謝。

------------------------------------- FOR REFERENCE ------- ------------------------------------
這裏是我的loadArray()函數:

int loadArray(char *inFileName, char ***array, int *count, int *capacity) 
{ 
FILE *inFile; 
char word[WORD_LENGTH]; /* this is the ONLY auto array we'll need */ 

if ((inFile = fopen(inFileName, "r")) == NULL) 
{ 
    fprintf(stderr,"Error opening input file, %s\n", inFileName); 
    return -1; 
} 

*array = (char **)malloc(*capacity * sizeof(char*)); 
if (*array == NULL) 
{ 
    fprintf(stderr, "Malloc of array in loadArray failed!\n"); 
    return -1; 
} 

printf("Reading file %s (each . is 5000 words read)\n", inFileName); 

*count = 0; 
while (fscanf(inFile, "%s", word) == 1) 
{ 
    if (*count >= *capacity) 
    { 
    /* call a function that will double the size of the array and copy its contents */ 
    doubleArray(array, count, capacity); 
    } 

    if (insertWord(*array, count, word) != 0) 
    { 
     fprintf(stderr," Insert returned an error!\n"); 
     fclose(inFile); 
     return 1; 
    } 

    if (*count % 5000 == 0) 
    { 
     printf("."); 
     fflush(stdout); /* stdout is buffered, so have to force flush */ 
    } 
} 

fclose(inFile); 

return 0; 
} 
+0

你也可以張貼'loadArray()'解決這一問題? – vidit

+0

完成。在我的帖子下方添加。 – Riptyde4

回答

2

如果*count是元素的array數(使用和未使用),那麼這

 array[*count+1] = array[*count]; 

將超越array界限。該array可能是指數從0到*計數 - 1

如果*count是使用的元素在array數量,你需要看的array總規模擴大了。

還有其他的指標array也可能> = = *。仔細看看他們。

如果在調用insertWord的代碼中使用malloc的話,您將需要realloc它調整array的大小。

在任何情況下,看到如何在調用insertWord的代碼中創建數組以便智能地進行註釋。


好的,新的信息。考慮容量= 100,count = 99的情況,你可以調用insertWord並且需要附加新的單詞。這

array[*count+1] = array[*count]; 

成爲

array[100] = array[99]; 

這100指數過大;容量爲100,有效指數爲0-99。 你可以通過改變

if (*count >= *capacity) // double array 

if (*count >= *capacity-1) // double array 
+0

是的,謝謝。 –

+0

我現在將loadArray()函數添加到我的帖子中。至於數組[[(count + 1)] = array [count]; - 這是因爲當將數組[I] =數組[I-1]移動到右邊時,我將覆蓋最後一個元素,這是我想要替換最後一個元素的原因,如果您知道一個更好的方式來做到這一點,請隨時分享:))。loadArray()函數負責重新分配,就像我在文章中看到的那樣。 – Riptyde4

+0

並確認,計數是元素的數量。 – Riptyde4

相關問題