如何解决 C 中涉及 void 指针的大小为 8 的无效 write/read

How to solve an Invalid write/read of size 8 involving a void pointer in C

我目前正在为一个项目处理哈希 table,我在内存清理方面遇到了一些问题。我正在使用 Valgrind,但收到此错误响应。

    ==1409499== Invalid write of size 8
    ==1409499==    at 0x4014F9: symtabInstall (symtab.c:106)
    ==1409499==    by 0x4011D0: main (test1.c:17)
    ==1409499==  Address 0x4a47128 is 0 bytes after a block of size 8 alloc'd
    ==1409499==    at 0x484086F: malloc (vg_replace_malloc.c:381)
    ==1409499==    by 0x4014B0: symtabInstall (symtab.c:102)
    ==1409499==    by 0x4011D0: main (test1.c:17)
    
    
    int symtabInstall(void *symtabHandle, const char *symbol, void *data){
        // Install a (symbol, data) pair in the table.
        // If the symbol is already installed in the table, then the data is
        //   overwritten.
        // If the symbol is not already installed, then space is allocated and
        //   a copy is made of the symbol, and the (symbol, data) pair is then
        //   installed in the table.
        // If successful, returns 1.
        // If memory cannot be allocated for a new symbol, then returns 0.
        // Note that no validation is made of the symbol table handle passed
        //   in. If not a valid handle, then the behavior is undefined (but
        //   probably bad).
        sym_t *symtable = symtabHandle;
        signed int location = search(symbol, symtable);
        if (location != -1)
            symtable->entries[location]->data = data;
        ///Create the input pair
        values *input = malloc(sizeof(input) * 1);
        if (input == NULL)
            return 0;
        input->symbol = malloc(strlen(symbol) + 1);
        input->data = data; /// This is the Line -------------------
        strcpy(input->symbol, symbol);
        ///Find spot to put in
        int symh = hash(symbol, symtable);
        ///Input check
        if (symtable->entries[symh] == NULL){
            symtable->entries[symh] = input;
        } else {
            int i = symh + 1;
            int c = 0;
            while(i < symtable->size){
                if (i == symtable->size && c == 0){
                    c = 1;
                } else if (c == 1){
                    return 0;
                }
                i %= symtable->size;
                if (symtable->entries[i] != NULL){
                    symtable->entries[symh] = input;
                    symtable->entries[symh]->data = data;
                }
                i++;
            }
        }
        return 1;
    }

对于上下文,输入是哈希的桶之一 table 并且有两个指针符号和数据。数据是给我的问题,因为我需要为它分配内存。 这是两者的结构。


    typedef struct values {
        char *symbol;
        void *data;
        struct values *next;
    } values;
    typedef struct{
        values **entries;
        int size;
    } sym_t;

我也不知道数据的数据类型。

看来问题出在这个内存分配上

values *input = malloc(sizeof(input) * 1);

我想你是说

values *input = malloc(sizeof( *input) * 1);

values *input = malloc(sizeof(values) * 1);

请注意,使用 multi[plier 1 没有多大意义。:)

您还忘记初始化分配对象的数据成员 next 类型值。

以及这个 while 循环的主体

 while(i < symtable->size){
     if (i == symtable->size && c == 0){
     // ...

也没有多大意义,因为以下 if 语句中的条件

i == symtable->size

永远不能计算为真。