小编Sea*_*her的帖子

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

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

这是我的哈希

#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 …
Run Code Online (Sandbox Code Playgroud)

c free hash dynamic-memory-allocation

-2
推荐指数
1
解决办法
44
查看次数

标签 统计

c ×1

dynamic-memory-allocation ×1

free ×1

hash ×1