如何解决涉及 C 中 void 指针的大小为 8 的无效写入/读取
我目前正在为一个项目开发哈希表,但在内存清理方面遇到了一些问题。我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来问题是这个内存分配
我认为你的意思是
或者
注意使用 multi[plier
1
没有多大意义。:)你还忘记初始化数据成员
类型值的已分配对象的下一个
。而且这个 while 循环的主体
也没有多大意义,因为下面的 if 语句中的条件
永远不能计算为 true。
It seems the problem is this memory allocation
I think you mean
or
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
also does not make a great sense because the condition in the following if statement
never can evaluate to true.