C free()不从结构数组中释放字符串
大家好,我正在创建一个程序,它使用散列存储文本文件中的单词及其出现次数。这正是我们想要的。我遇到的问题来自释放分配的内存 这是我的杂烩C free()不从结构数组中释放字符串,c,hash,free,dynamic-memory-allocation,C,Hash,Free,Dynamic Memory Allocation,大家好,我正在创建一个程序,它使用散列存储文本文件中的单词及其出现次数。这正是我们想要的。我遇到的问题来自释放分配的内存 这是我的杂烩 #include<stdio.h> #include<stdlib.h> #include<string.h> #include<ctype.h> #include"hash.h" /* struct listnode{ char * word; int count; }; */ void ha
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include"hash.h"
/*
struct listnode{
char * word;
int count;
};
*/
void hashCreate(struct listnode * hashTable[], int size){
int i;
for(i=0;i<size;i++){
hashTable[i]=(struct listnode *)malloc(sizeof(struct listnode));
hashTable[i]->count=0;
}
}
int hash(char * data, int size) {
unsigned long hash = 5381;
char * p;
for (p = data; *p != '\0'; ++p) {
hash = (hash * 33) + *p;
}
return (int)(hash % size);
}
void hashAdd(char * data, struct listnode * hashTable[], int size){
int key=hash(data, size);
hashTable[key]->word=strdup(data);
hashTable[key]->count+=1;
}
void hashPrint(struct listnode * hashTable[], int size){
int i;
for(i=0;i<size;i++){
if(hashTable[i]->count!=0)
printf("%s: %d \n",hashTable[i]->word,hashTable[i]->count);
}
}
void hashDelete(struct listnode * hashTable[],int size){
int i;
for(i=0;i<size;i++){
free(hashTable[i]->word);
free(hashTable[i]);
}
}
#包括
#包括
#包括
#包括
#包括“hash.h”
/*
结构列表节点{
字符*字;
整数计数;
};
*/
void hashCreate(结构listnode*哈希表[],整数大小){
int i;
对于(i=0;icount=0;
}
}
整数散列(字符*数据,整数大小){
无符号长散列=5381;
char*p;
对于(p=data;*p!='\0';++p){
散列=(散列*33)+*p;
}
返回值(int)(哈希%size);
}
void hashAdd(字符*数据,结构listnode*哈希表[],整数大小){
int key=hash(数据、大小);
哈希表[key]->word=strdup(数据);
哈希表[键]->计数+=1;
}
void hashPrint(结构listnode*哈希表[],整数大小){
int i;
对于(i=0;i计数!=0)
printf(“%s:%d\n”,哈希表[i]->字,哈希表[i]->计数);
}
}
void hashDelete(结构listnode*哈希表[],整数大小){
int i;
对于(i=0;iword);
自由(哈希表[i]);
}
}
这就是它的用途
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include"hash.h"
/*
int hash(char * data, int size) {
unsigned long hash = 5381;
char * p;
for (p = data; *p != '\0'; ++p) {
hash = (hash * 33) + *p;
}
return (int)(hash % size);
}
*/
#define SIZE 1500
void removePunct(char * str);
void fileRead(char * filename);
struct listnode * hashTable[1500];
int main(int argc, char ** argv){
int i;
if(argc<2)
fprintf(stderr,"Enter filename \n");
hashCreate(hashTable, SIZE);
for(i=1; i<argc; i++){
fileRead(argv[i]);
}
hashPrint(hashTable,SIZE);
hashDelete(hashTable, SIZE);
return 0;
}
void fileRead(char * filename){
FILE * file = fopen(filename,"r");
char word[80];
if(!file){
fprintf(stderr,"Error opening file \n");
return;
}
while(fscanf(file, "%s", word)==1){
removePunct(word);
hashAdd(word,hashTable,SIZE);
}
fclose(file);
}
void removePunct(char * str){
int i,p=0;
for(i=0; i<strlen(str);i++){
if(isalpha(str[i]) || str[i]==' '){
str[p]=tolower(str[i]);
p++;
}
}
str[p]='\0';
}
#包括
#包括
#包括
#包括
#包括“hash.h”
/*
整数散列(字符*数据,整数大小){
无符号长散列=5381;
char*p;
对于(p=data;*p!='\0';++p){
散列=(散列*33)+*p;
}
返回值(int)(哈希%size);
}
*/
#定义尺寸1500
无效删除点(char*str);
无效文件读取(字符*文件名);
结构listnode*哈希表[1500];
int main(int argc,字符**argv){
int i;
如果(argc在该代码中
void hashAdd(char * data, struct listnode * hashTable[], int size){
int key=hash(data, size);
hashTable[key]->word=strdup(data);
hashTable[key]->count+=1;
}
您使用strdup
获取一个新字符串(由strdup malloc'ed)。如果您已经为给定的键
这样做过一次,您将泄漏内存
因此,您需要进行如下检查:
if (hashTable[key]->word == NULL) hashTable[key]->word=strdup(data);
但是,这需要在创建表时将word
初始化为NULL
主题:通常情况下,您需要使用一些额外的代码来处理相同的键
值。导致该键
的数据
值可能与已存储的字
相同,也可能不同。您应该检查一些内容。如果它们相同,您可以增加计数
你必须有一种方法来存储两个具有相同关键字值的不同单词
它可能看起来像:
void hashAdd(char * data, struct listnode * hashTable[], int size){
int key=hash(data, size);
if (hashTable[key]->word == NULL) {
// First time with this key
hashTable[key]->word=strdup(data);
hashTable[key]->count+=1;
} else {
// Key already used once
if (strcmp(data, hashTable[key]->word) == 0) {
// Same word
hashTable[key]->count+=1;
} else {
// Different word
// ...
// Add code for storing this word in another location
// ...
}
}
}
谢谢。我知道这一定很简单,我没有注意到。这就解决了它。