2013-10-18 24 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

相关问题