请大家帮我看看这个程序,free(): invalid pointer

diana_cherry 2013-04-26 04:55:27
写了一个hash程序,总是在recreate_hash的时候free这行报free(): invalid pointer,我用gdb跟踪,在free之前tmp[i].key是有值的,看上去也很正常,程序源码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

unsigned int BKDRHash(char *str);
typedef char *KeyType;
typedef struct{
KeyType key;
int count;
}ElemType;
typedef struct{
ElemType *elem;
unsigned int size;
unsigned int real_size;
}HashTable;
void error_and_die(const char *msg){
perror(msg);
exit(EXIT_FAILURE);
}

unsigned int BKDRHash(char *str){
unsigned int seed = 31; // 31 131 1313 13131 131313 etc..
unsigned int hash = 0;
unsigned int i = 0;
while (*(str+i)) {
hash = (hash * seed + (*str+i)) ;
i++;
}
return hash;
}
unsigned int insert_hash(HashTable *ht,char *key){
unsigned int collision_num=0;
unsigned int p=BKDRHash(key)%ht->size;;
while(ht->elem[p].key!=NULL && strcmp(ht->elem[p].key,key)!=0){
collision_num++;
if(collision_num==(ht->size-ht->real_size)) return -1;
//平方探测法,f(i)=i^2=f(i-1)+2i-1
p+=2*collision_num-1;
if(p>ht->size) p-=ht->size;
}
if(ht->elem[p].key==NULL){
//ht->elem[p].key=strdup(key);
ht->elem[p].key=calloc(sizeof(char),(strlen(key)+1));
strcpy(ht->elem[p].key,key);
ht->elem[p].count=1;
ht->real_size++;
}else if(strcmp(ht->elem[p].key,key)==0){
ht->elem[p].count++;
}
return collision_num;
}
unsigned int insert_hash_with_count(HashTable *ht,char *key,int count){
unsigned int collision_num=0;
unsigned int p=BKDRHash(key)%ht->size;;
while(ht->elem[p].key!=NULL && strcmp(ht->elem[p].key,key)!=0){
collision_num++;
p+=2*collision_num-1;
}
//ht->elem[p].key=strdup(key);
ht->elem[p].key=calloc(sizeof(char),(strlen(key)+1));
strcpy(ht->elem[p].key,key);
//free(key);
ht->elem[p].count=count;
ht->real_size++;
return collision_num;
}
void init_hash(HashTable *ht,int size){
ht->size=size;
ht->elem=malloc(sizeof(ElemType)*ht->size);
ht->real_size=0;
}
void recreate_hash(HashTable *ht,int size){
ElemType *tmp=ht->elem;
unsigned int old_size=ht->size;
init_hash(ht,size);
unsigned int i=0;
for(i=0;i<old_size;i++){
if(tmp[i].key!=NULL)
{
insert_hash_with_count(ht,tmp[i].key,tmp[i].count);
free(tmp[i].key);
}
}
free(tmp);
}
int main(int argc, char *argv[]){
unsigned int hashsize[]={5,101,100057,2000003,10000019,20000047,100000007};
HashTable *ht=malloc(sizeof(HashTable));
int step=0;
init_hash(ht,hashsize[step]);
int i=0;
char line[8192];
int p;
while(fgets(line,8092,stdin)){
line[strlen(line)-1]='\0';
if(ht->real_size > (int)ht->size*0.75){
recreate_hash(ht,hashsize[++step]);
p=insert_hash(ht,line);
continue;
}
int p=insert_hash(ht,line);
if(p==-1){
recreate_hash(ht,hashsize[++step]);
p=insert_hash(ht,line);
}
}
for(i=0;i<ht->size;i++)
{
if(ht->elem[i].key!=NULL)
printf("%s\t%d\n",ht->elem[i].key,ht->elem[i].count);
free(ht->elem[i].key);
}
free(ht->elem);
free(ht);

}



测试文件内容:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NULLKEY NULL
typedef struct{
unsigned int BKDRHash(char *str);
typedef char KeyType[8192];
typedef struct{
KeyType key;
int count;
#include <stdio.h>
...全文
616 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
diana_cherry 2013-04-27
  • 打赏
  • 举报
回复
引用 2 楼 wadehao 的回复:
程序问题很多,tmp得用二级指针,free(tmp.key[i])这个你想干嘛??
我想把以前那个hash的key释放了
赵4老师 2013-04-27
  • 打赏
  • 举报
回复
无malloc,无free A=malloc,free(A)
winassembly 2013-04-26
  • 打赏
  • 举报
回复
free()函数只用来释放由malloc,calloc动态分配的存储空间,否则的话就会出错。
eureka_cs 2013-04-26
  • 打赏
  • 举报
回复
程序问题很多,tmp得用二级指针,free(tmp.key[i])这个你想干嘛??
nirvana_newbie 2013-04-26
  • 打赏
  • 举报
回复
tmp不是用malloc动态生成的,怎么能用free呢?

70,022

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧