如何解决涉及 C 中 void 指针的大小为 8 的无效写入/读取

发布于 2025-01-11 01:49:09 字数 2805 浏览 0 评论 0原文

我目前正在为一个项目开发哈希表,但在内存清理方面遇到了一些问题。我正在使用 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;
    }

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


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

我也不了解数据的数据类型。

I am currently working on a hash table for a project and I am having some trouble with the memory cleanup. I am using Valgrind and I am getting this error response.

    ==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;
    }

For context, input is one of the buckets for the hash table and has two pointers symbol and data. Data is the one giving me the issue as I need to allocate memory for it.
Here are the structs for both.


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

I am also given no knowledge of the data type for data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生生不灭 2025-01-18 01:49:09

看来问题是这个内存分配

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

我认为你的意思是

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

或者

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

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

你还忘记初始化数据成员 类型值的已分配对象的下一个

而且这个 while 循环的主体

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

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

i == symtable->size

永远不能计算为 true。

It seems the problem is this memory allocation

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

I think you mean

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

or

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

Pay attention to that using the multi[plier 1 does not make a great sense.:)

Also you forgot to initialize the data member next of the allocated object of the type values.

And the body of this while loop

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

also does not make a great sense because the condition in the following if statement

i == symtable->size

never can evaluate to true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文