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)

如果有人能指出我正确的方向,将不胜感激,谢谢。

- - - - - - - - - - - - - - - - - - -以供参考 - - - - - - --------------------------------
这是我的 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;
}
4

1 回答 1

2

如果*countarray(已使用和未使用)中的元素数量,则此

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

会超越的界限arrayarray可能是从 0 到 *count - 1的索引。

如果*count是 中使用的元素数array,则需要查看array扩展前的总大小。

还有其他索引array也可能是 >= *count。仔细看看他们。

如果在调用的代码中对数组进行了 malloc'ed,insertWord您将需要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
于 2013-10-18T18:06:40.323 回答