gpt4 book ai didi

c - free() 不从结构数组中释放字符串

转载 作者:行者123 更新时间:2023-11-30 19:57:44 26 4
gpt4 key购买 nike

嗨,我正在创建一个程序,它使用哈希来存储文本文件中的单词及其出现次数。这按预期工作。我遇到的问题来自于释放分配的内存。

这是我的哈希

#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]);
}
}

这就是它的用途

#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';
}

在我的 hashDelete 函数中,字符串没有被释放,这导致了内存泄漏。我通过释放 hashAdd 函数中的字符串来测试它,没有内存泄漏,但也没有打印任何字符串。我无法找到不让我释放所有内存的问题。任何帮助将不胜感激。

最佳答案

在此代码中

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)。如果您已经对给定的key执行过一次,您将泄漏内存。

所以你需要这样的检查:

if (hashTable[key]->word == NULL) hashTable[key]->word=strdup(data);

但是,这要求您在创建表时将 word 初始化为 NULL。

主题:通常,您需要使用一些额外的代码来处理相同的key值。产生该keydata值可能与已存储的word相同,也可能不同。你应该检查一下。如果它们相同,您可以增加 count。如果它们不同,您就必须有一种方法来存储具有相同 key 值的两个不同单词。

它可能看起来像:

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
// ...
}
}
}

关于c - free() 不从结构数组中释放字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38518923/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com