返回信息流代码如下:
in hashTable.h:
#ifndef _HASHTABLE_H
#define _HASHTABLE_H
#define SIZE_STRING 64
typedef unsigned int (*hash_function)(char*, unsigned int len);
typedef char string[SIZE_STRING];
typedef struct _hashnode{
int index;
int next;
}HashNode,*pHashNode;
typedef struct _hashtable{
int hashSize;
int confSize;
int confCount;
hash_function hash_func;
pHashNode hashArea;
pHashNode confArea;
int dataSize;
int dataCount;
string * data;
}HashTable,*pHashTable;
int pushString(char * str);
int hashTable_init();
int hashTable_insert(string s);
int hashTable_destory();
int hashTable_print();
#endif
in hashTable.c:
#include<stdio.h>
#include<stdio.h>
#include<string.h>
#include"GeneralHashFunctions.h"
#include"hashTable.h"
#define TABLE_SIZE 5
#define INVALID -1
pHashTable pht;
/*int hashTable_init(pHashTable pht){*/
int hashTable_init(){
pht=(pHashTable)malloc(sizeof(HashTable));
pht->hashSize = TABLE_SIZE;
pht->confSize = TABLE_SIZE;
pht->confCount = 0;
pht->hash_func = &RSHash;
pht->hashArea = (pHashNode)malloc(sizeof(HashNode)*pht->hashSize);
pht->confArea = (pHashNode)malloc(sizeof(HashNode)*pht->confSize);
pht->dataSize = 5;
pht->dataCount = 0;
pht->data = (string*)calloc(pht->dataSize,sizeof(string));
memset(pht->hashArea,INVALID,sizeof(HashNode)*pht->hashSize);
memset(pht->confArea,INVALID,sizeof(HashNode)*pht->confSize);
/* memset(pht->data,0,sizeof(string) *pht->dataSize);*/
/* hashTable_sem = sem_init(&hashTable_sem,0,1);*/
printf("dataSize=%d\ndataCount=%d\n",pht->dataSize,pht->dataCount);
printf("pht=%d",pht);
return 0;
}
/*int hashTable_insert(pHashTable pht,string s){*/
int hashTable_insert(string s){
int hv = 0,temphv;
pHashNode temp=NULL;
temphv = hv = pht->hash_func(s,strlen(s))%TABLE_SIZE;
printf("hv=%d\n",hv);
if(INVALID==pht->hashArea[hv].index){
pht->hashArea[hv].index=pht->dataCount;
/* pushString(pht,s);*/
pushString(s);
}else{
if(0==strcmp(pht->data[pht->hashArea[hv].index],s))return -1;
hv = pht->hashArea[hv].next;
while(INVALID!=pht->confArea[hv].index){
if(0==strcmp(pht->data[pht->confArea[hv].index],s))return -1;
if(INVALID!= pht->confArea[hv].next){
hv=pht->confArea[hv].next;
}else{
if(pht->hashArea[temphv].next==INVALID){
printf("in\n");
pht->hashArea[temphv].next=pht->confCount;break;
}else{
pht->confArea[hv].next=pht->confCount;break;
}
}
}
if(pht->hashArea[temphv].next==INVALID){
pht->hashArea[temphv].next=pht->confCount;
}
pht->confArea[pht->confCount].index=pht->dataCount;
pht->confCount++;
if(pht->confCount>=pht->confSize){
printf("realloc conf\n");
temp=pht->confArea=(pHashNode)realloc(pht->confArea,pht->confSize*2);
if(!temp){printf("realloc failed confArea!\n");}else{printf("realloc success confArea!\n");}
pht->confArea=temp;
memset(pht->confArea+pht->confSize,INVALID,sizeof(HashNode)*pht->confSize);
pht->confSize*=2;
printf("confSize=%d;confCount=%d;\n",pht->confSize,pht->confCount);
printf("dataSize=%d;dataCount=%d;\n",pht->dataSize,pht->dataCount);
printf("realloc conf\n");
}
printf("before push\n");
/* pushString(pht,s);*/
pushString(s);
}
hashTable_print(pht);
return 0;
}
/*int pushString(pHashTable pht,string s){*/
int pushString(string s){
string * temp=NULL;
printf("pht->data=%x\n;malloc_usable_size(pht->data)=%d\n",pht->data,malloc_usable_size(pht->data));
strcpy(pht->data[pht->dataCount++],s);
printf("pht->dataCount=%d",pht->dataCount);
if(pht->dataCount>=pht->dataSize){
printf("realloc data");
temp =(string *)realloc(pht->data,sizeof(string)*pht->dataSize*2);
printf("temp=%x\n",temp);
if(!temp){
printf("realloc failed!");
}else{
printf("realloc success");
pht->data = temp;
}
printf("afterrealloc\n");
memset(pht->data+pht->dataSize,0,sizeof(string)*pht->dataSize);
printf("aftermemset\n");
pht->dataSize*=2;
printf("realloc data");
}
return 0;
}
/*
int hashTable_delete(pHashTable pht){
return 0;
}*/
/*int hashTable_destory(pHashTable pht){*/
int hashTable_destory(){
free(pht->hashArea);
free(pht->confArea);
free(pht->data);
return 0;
}
/*int hashTable_print(pHashTable pht){*/
int hashTable_print(){
int i,j;
printf("confSize=%d\nconfCount=%d\n",pht->confSize,pht->confCount);
for(i=0;i<pht->hashSize;i++){
printf("hashArea[%d]->index:%d;next:%d;\n",i,pht->hashArea[i].index,pht->hashArea[i].next);
}
for(i=0;i<pht->confSize;i++){
printf("confArea[%d]->index:%d;next:%d;\n",i,pht->confArea[i].index,pht->confArea[i].next);
}
printf("datasize=%d;datacount=%d;\n",pht->dataSize,pht->dataCount);
for(j=0;j<pht->dataSize;j++){
printf("data[%d]=%s;\n",j,pht->data[j]);
}
return 0;
}
in main.c:
#include<stdio.h>
#include"hashTable.h"
int main(){
int res;
hashTable_init();
printf("%d\n\n",hashTable_insert("Hello"));
printf("%d\n\n",hashTable_insert("NO"));
printf("%d\n\n",hashTable_insert("Yes"));
printf("%d\n\n",hashTable_insert("GOOD"));
printf("%d\n\n",hashTable_insert("BAD"));
printf("%d\n\n",hashTable_insert("OOD"));
printf("%d\n\n",hashTable_insert("OD"));
printf("%d\n\n",hashTable_insert("BYRBYR"));
printf("%d\n\n",hashTable_insert("BYEBYE"));
printf("%d\n\n",hashTable_insert("OMG"));
printf("%d\n\n",hashTable_insert("DHT"));
printf("%d\n\n",hashTable_insert("FMT"));
printf("%d\n\n",hashTable_insert("WTF"));
printf("%d\n\n",hashTable_insert("PP"));
hashTable_destory();
return 0;
}
第一次调用pushString时,没有出现错误,但是第二次调用pushString时出现了invalid pointer
错误信息如下:
*** glibc detected *** ./ht: realloc(): invalid pointer: 0x0856e090 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x75ee2)[0xb7604ee2]
/lib/i386-linux-gnu/libc.so.6(realloc+0x25d)[0xb760956d]
./ht[0x8048acb]
./ht[0x8048a00]
./ht[0x8048e07]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb75a84d3]
./ht[0x80484a1]
======= Memory map: ========
08048000-0804a000 r-xp 00000000 08:06 1318007 /home/hl/new/testHash/ht
0804a000-0804b000 r--p 00001000 08:06 1318007 /home/hl/new/testHash/ht
0804b000-0804c000 rw-p 00002000 08:06 1318007 /home/hl/new/testHash/ht
0856e000-0858f000 rw-p 00000000 00:00 0 [heap]
b755e000-b757a000 r-xp 00000000 08:06 787285 /lib/i386-linux-gnu/libgcc_s.so.1
b757a000-b757b000 r--p 0001b000 08:06 787285 /lib/i386-linux-gnu/libgcc_s.so.1
b757b000-b757c000 rw-p 0001c000 08:06 787285 /lib/i386-linux-gnu/libgcc_s.so.1
b758e000-b758f000 rw-p 00000000 00:00 0
b758f000-b7733000 r-xp 00000000 08:06 790387 /lib/i386-linux-gnu/libc-2.15.so
b7733000-b7735000 r--p 001a4000 08:06 790387 /lib/i386-linux-gnu/libc-2.15.so
b7735000-b7736000 rw-p 001a6000 08:06 790387 /lib/i386-linux-gnu/libc-2.15.so
b7736000-b7739000 rw-p 00000000 00:00 0
b7749000-b774d000 rw-p 00000000 00:00 0
b774d000-b774e000 r-xp 00000000 00:00 0 [vdso]
b774e000-b776e000 r-xp 00000000 08:06 790377 /lib/i386-linux-gnu/ld-2.15.so
b776e000-b776f000 r--p 0001f000 08:06 790377 /lib/i386-linux-gnu/ld-2.15.so
b776f000-b7770000 rw-p 00020000 08:06 790377 /lib/i386-linux-gnu/ld-2.15.so
bf94a000-bf96b000 rw-p 00000000 00:00 0 [stack]
求各位大牛解答,这个错误信息(memory map)我也看不懂……
这是一条镜像帖。来源:北邮人论坛 / cpp / #75222同步于 2013/11/16
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
realloc得到的内存,再次realloc时出现invalid pointer【已解决
byr304225
2013/11/16镜像同步3 回复
订阅后,新回复会通过你的通知中心匿名送达。
3 条回复
【自问自答】果然不是realloc的问题
if(pht->confCount>=pht->confSize){
temp=pht->confArea=(pHashNode)realloc(pht->confArea,sizeof(HashNode)*pht->confSize*2);//这个地方一开始没有乘sizeof(HashNode)
if(!temp){
printf("realloc failed confArea!\n");
}else{
pht->confArea=temp;
}
memset(pht->confArea+pht->confSize,INVALID,sizeof(HashNode)*pht->confSize);
pht->confSize*=2;
}
【怎么发现的】
malloc_usable_size(pht->data);
malloc_usable_size(pht->confArea);
这个函数返回某某指针被分配的内存块大小