69,382
社区成员
发帖
与我相关
我的任务
分享
bool WSetValue(char *Name, float value)
{
struct TTT *t;
if ((t = (struct TTT*)hash_table_get(ht, Name)) == NULL)
return false;
//Do Something。。。。
free(t); //这里是否必须free掉这个结构体指针?
return true;
}
//将c:\\tmp文件夹下的所有文件的内容全部放到用malloc分配的内存中
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
struct FB {
char fn[256];
size_t fl;
char *b;
struct FB *next;
struct FB *prev;
} *fh,*fb,*ft;
char ln[256];
char fpn[256];
FILE *af;
FILE *f;
int L,n;
int main() {
system("dir /b /a-d c:\\tmp\\*.* >c:\\allfn.txt");
af=fopen("c:\\allfn.txt","r");
if (NULL==af) {
printf("Can not open file c:\\allfn.txt!\n");
return 1;
}
fh=NULL;
fb=NULL;
n=0;
while (1) {
if (NULL==fgets(ln,256,af)) break;
L=strlen(ln);
if ('\n'==ln[L-1]) ln[L-1]=0;
printf("read %s\n",ln);
strcpy(fpn,"c:\\tmp\\");
strcat(fpn,ln);
ft=(struct FB *)malloc(sizeof(struct FB));
if (NULL==ft) {
printf("Can not malloc ft!\n");
fclose(af);
return 2;//之前的malloc在main退出后由操作系统自动free
}
printf("ft[%d]==%p\n",n,ft);
strcpy(ft->fn,fpn);
f=fopen(fpn,"rb");
if (NULL==f) {
printf("Can not open file %s!\n",fpn);
fclose(af);
return 3;//之前的malloc在main退出后由操作系统自动free
}
ft->fl=_filelength(fileno(f));
ft->b=malloc(ft->fl);
if (NULL==ft->b) {
printf("Can not malloc ft->b!\n");
fclose(f);
fclose(af);
return 4;//之前的malloc在main退出后由操作系统自动free
}
printf("ft[%d]->b==%p\n",n,ft->b);
if (ft->fl!=fread(ft->b,1,ft->fl,f)) {
printf("fread error!\n");
fclose(f);
fclose(af);
return 5;//之前的malloc在main退出后由操作系统自动free
}
fclose(f);
ft->next=NULL;
if (NULL==fh) {
ft->prev=NULL;
fh=ft;
} else {
fb->next=ft;
ft->prev=fb;
}
fb=ft;
n++;
}
fclose(af);
printf("-----list-----\n");
for (ft=fh;NULL!=ft;ft=ft->next) {
printf("%8d %s\n",ft->fl,ft->fn);
if (NULL!=ft) fb=ft;
}
printf("-----free-----\n");
n--;
if (NULL!=fh) {
for (ft=fb->prev;NULL!=ft;ft=ft->prev) {
if (NULL!=ft->next->b) {
printf("ft[%d]->b==%p\n",n,ft->next->b);
free(ft->next->b);
}
if (NULL!=ft->next) {
printf("ft[%d]==%p\n",n,ft->next);
free(ft->next);
}
n--;
}
if (NULL!=fh->b) {
printf("ft[0]->b==%p\n",fh->b);
free(fh->b);
}
printf("ft[0]==%p\n",fh);
free(fh);
}
return 0;
}
//C:\tmp\tmp\Debug>dir /a-d c:\tmp
// 驱动器 C 中的卷是 C_HD5_1
// 卷的序列号是 1817-D526
//
// c:\tmp 的目录
//
//找不到文件
//
//C:\tmp\tmp\Debug>tmp
//找不到文件
//-----list-----
//-----free-----
//
//C:\tmp\tmp\Debug>dir /a-d c:\tmp
// 驱动器 C 中的卷是 C_HD5_1
// 卷的序列号是 1817-D526
//
// c:\tmp 的目录
//
//2011-06-30 18:04 44,840 my_c.rar
//2011-06-30 17:18 1,036 err.frm
//2011-06-30 14:32 14,243 出租.txt
//2011-06-28 12:08 23,681 MSDN98书签.txt
// 4 个文件 83,800 字节
// 0 个目录 17,041,870,848 可用字节
//
//C:\tmp\tmp\Debug>tmp
//read my_c.rar
//ft[0]==00421800
//ft[0]->b==00520068
//read err.frm
//ft[1]==00421670
//ft[1]->b==0052AFC0
//read 出租.txt
//ft[2]==00421530
//ft[2]->b==00378F28
//read MSDN98书签.txt
//ft[3]==004213F0
//ft[3]->b==0052B3F8
//-----list-----
// 44840 c:\tmp\my_c.rar
// 1036 c:\tmp\err.frm
// 14243 c:\tmp\出租.txt
// 23681 c:\tmp\MSDN98书签.txt
//-----free-----
//ft[3]->b==0052B3F8
//ft[3]==004213F0
//ft[2]->b==00378F28
//ft[2]==00421530
//ft[1]->b==0052AFC0
//ft[1]==00421670
//ft[0]->b==00520068
//ft[0]==00421800
//
//C:\tmp\tmp\Debug>
再供参考:
十字链表交换任意两个节点C源代码(C指针应用终极挑战)http://download.csdn.net/detail/zhao4zhong1/5532495
#include "HashTable.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define TABLE_SIZE (1024*1024)
/* element of the hash table's chain list */
struct kv
{
struct kv* next;
char* key;
void* value;
void(*free_value)(void*);
};
/* HashTable */
struct HashTable
{
struct kv ** table;
};
/* constructor of struct kv */
static void init_kv(struct kv* kv)
{
kv->next = NULL;
kv->key = NULL;
kv->value = NULL;
kv->free_value = NULL;
}
/* destructor of struct kv */
static void free_kv(struct kv* kv)
{
if (kv) {
if (kv->free_value) {
kv->free_value(kv->value);
}
free(kv->key);
kv->key = NULL;
free(kv);
}
}
/* the classic Times33 hash function */
static unsigned int hash_33(char* key)
{
unsigned int hash = 0;
while (*key) {
hash = (hash << 5) + hash + *key++;
}
return hash;
}
/* new a HashTable instance */
HashTable* hash_table_new()
{
HashTable* ht = malloc(sizeof(HashTable));
if (NULL == ht) {
hash_table_delete(ht);
return NULL;
}
ht->table = malloc(sizeof(struct kv*) * TABLE_SIZE);
if (NULL == ht->table) {
hash_table_delete(ht);
return NULL;
}
memset(ht->table, 0, sizeof(struct kv*) * TABLE_SIZE);
return ht;
}
/* delete a HashTable instance */
void hash_table_delete(HashTable* ht)
{
if (ht) {
if (ht->table) {
int i = 0;
for (i = 0; i<TABLE_SIZE; i++) {
struct kv* p = ht->table[i];
struct kv* q = NULL;
while (p) {
q = p->next;
free_kv(p);
p = q;
}
}
free(ht->table);
ht->table = NULL;
}
free(ht);
}
}
/* insert or update a value indexed by key */
int hash_table_put2(HashTable* ht, char* key, void* value, void(*free_value)(void*))
{
int i = hash_33(key) % TABLE_SIZE;
struct kv* p = ht->table[i];
struct kv* prep = p;
while (p) { /* if key is already stroed, update its value */
if (strcmp(p->key, key) == 0) {
if (p->free_value) {
p->free_value(p->value);
}
p->value = value;
p->free_value = free_value;
break;
}
prep = p;
p = p->next;
}
if (p == NULL) {/* if key has not been stored, then add it */
char* kstr = malloc(strlen(key) + 1);
if (kstr == NULL) {
return -1;
}
struct kv * kv = malloc(sizeof(struct kv));
if (NULL == kv) {
free(kstr);
kstr = NULL;
return -1;
}
init_kv(kv);
kv->next = NULL;
strcpy(kstr, key);
kv->key = kstr;
kv->value = value;
kv->free_value = free_value;
if (prep == NULL) {
ht->table[i] = kv;
}
else {
prep->next = kv;
}
}
return 0;
}
/* get a value indexed by key */
void* hash_table_get(HashTable* ht, char* key)
{
int i = hash_33(key) % TABLE_SIZE;
struct kv* p = ht->table[i];
while (p) {
if (strcmp(key, p->key) == 0) {
return p->value;
}
p = p->next;
}
return NULL;
}
/* remove a value indexed by key */
void hash_table_rm(HashTable* ht, char* key)
{
int i = hash_33(key) % TABLE_SIZE;
struct kv* p = ht->table[i];
struct kv* prep = p;
while (p) {
if (strcmp(key, p->key) == 0) {
free_kv(p);
if (p == prep) {
ht->table[i] = NULL;
}
else {
prep->next = p->next;
}
}
prep = p;
p = p->next;
}
}
赵老师您好,hash_table代码如上, 这种情况下我需要free么?[/quote]
但从这个函数功能上看,不需要释放,因为一旦释放,那么下次就不能查到了,也可能影响其他哈希值的操作。再说t只是一个指针,并没有占用或者malloc申请空间。
并且看到有专门的释放操作,因此若要释放建议使用专门的接口
对于是否释放,最终要看是否继续使用了,从函数上来看,并不是释放而是查询得到哈希值,因此个人认为不需要释放。
#include "HashTable.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define TABLE_SIZE (1024*1024)
/* element of the hash table's chain list */
struct kv
{
struct kv* next;
char* key;
void* value;
void(*free_value)(void*);
};
/* HashTable */
struct HashTable
{
struct kv ** table;
};
/* constructor of struct kv */
static void init_kv(struct kv* kv)
{
kv->next = NULL;
kv->key = NULL;
kv->value = NULL;
kv->free_value = NULL;
}
/* destructor of struct kv */
static void free_kv(struct kv* kv)
{
if (kv) {
if (kv->free_value) {
kv->free_value(kv->value);
}
free(kv->key);
kv->key = NULL;
free(kv);
}
}
/* the classic Times33 hash function */
static unsigned int hash_33(char* key)
{
unsigned int hash = 0;
while (*key) {
hash = (hash << 5) + hash + *key++;
}
return hash;
}
/* new a HashTable instance */
HashTable* hash_table_new()
{
HashTable* ht = malloc(sizeof(HashTable));
if (NULL == ht) {
hash_table_delete(ht);
return NULL;
}
ht->table = malloc(sizeof(struct kv*) * TABLE_SIZE);
if (NULL == ht->table) {
hash_table_delete(ht);
return NULL;
}
memset(ht->table, 0, sizeof(struct kv*) * TABLE_SIZE);
return ht;
}
/* delete a HashTable instance */
void hash_table_delete(HashTable* ht)
{
if (ht) {
if (ht->table) {
int i = 0;
for (i = 0; i<TABLE_SIZE; i++) {
struct kv* p = ht->table[i];
struct kv* q = NULL;
while (p) {
q = p->next;
free_kv(p);
p = q;
}
}
free(ht->table);
ht->table = NULL;
}
free(ht);
}
}
/* insert or update a value indexed by key */
int hash_table_put2(HashTable* ht, char* key, void* value, void(*free_value)(void*))
{
int i = hash_33(key) % TABLE_SIZE;
struct kv* p = ht->table[i];
struct kv* prep = p;
while (p) { /* if key is already stroed, update its value */
if (strcmp(p->key, key) == 0) {
if (p->free_value) {
p->free_value(p->value);
}
p->value = value;
p->free_value = free_value;
break;
}
prep = p;
p = p->next;
}
if (p == NULL) {/* if key has not been stored, then add it */
char* kstr = malloc(strlen(key) + 1);
if (kstr == NULL) {
return -1;
}
struct kv * kv = malloc(sizeof(struct kv));
if (NULL == kv) {
free(kstr);
kstr = NULL;
return -1;
}
init_kv(kv);
kv->next = NULL;
strcpy(kstr, key);
kv->key = kstr;
kv->value = value;
kv->free_value = free_value;
if (prep == NULL) {
ht->table[i] = kv;
}
else {
prep->next = kv;
}
}
return 0;
}
/* get a value indexed by key */
void* hash_table_get(HashTable* ht, char* key)
{
int i = hash_33(key) % TABLE_SIZE;
struct kv* p = ht->table[i];
while (p) {
if (strcmp(key, p->key) == 0) {
return p->value;
}
p = p->next;
}
return NULL;
}
/* remove a value indexed by key */
void hash_table_rm(HashTable* ht, char* key)
{
int i = hash_33(key) % TABLE_SIZE;
struct kv* p = ht->table[i];
struct kv* prep = p;
while (p) {
if (strcmp(key, p->key) == 0) {
free_kv(p);
if (p == prep) {
ht->table[i] = NULL;
}
else {
prep->next = p->next;
}
}
prep = p;
p = p->next;
}
}
赵老师您好,hash_table代码如上, 这种情况下我需要free么?