关于free()哈希表的问题 [英] Question on free() ing a hash table

查看:110
本文介绍了关于free()哈希表的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

练习6-5

编写一个functon undef,它将从通过查找和安装维护的

表中删除名称和定义。


代码:


unsigned hash(char * s);


void undef(char * s)

{

int h;

struct nlist * prev,* np;


prev = NULL;

h = hash(s);

for(np = hashtab [h]; np!= NULL; np = np-> next){

if(strcmp(s,np-> name)== 0)

break;

prev = np;

}


if(np!= NULL){

if(prev == NULL)

hashtab [h] = np-> next;

else

prev-> next = np-> next;

free((void *)np - >名称);

免费((void *)np-> defn);

免费((void *)np);

}

}


问题是,为什么我必须释放()np-> name和np-> defn?

即,我怎么不能自由()np?

解决方案

Chad写道:


练习6-5


写一个功能undef将从查询和安装维护的

表中删除名称和定义。


代码:


unsigned hash(char * s);


void undef(char * s)

{

int h;

struct nlist * prev,* np;


prev = NULL;

h = hash(s);

for(np = hashtab [h]; NP!= NULL; np = np-> next){

if(strcmp(s,np-> name)== 0)

break;

prev = np;

}


if(np!= NULL){

if(prev == NULL)

hashtab [h] = np-> next;

else

prev-> next = np-> next;

free((void *)np-> name);

free((void *)np-> defn);

free(( void *)np);

}

}


问题是,为什么我必须释放()两个np - > name和np-> defn?

即,我怎么不能自由()np?



因为它们的内存是单独分配的。

每次调用malloc()都需要调用free()。你构建了

struct np,其中三次调用malloc - 一次用于struct本身,

另外两次用来初始化它''的成员''name''和'' defn''指向

可用存储空间。因此,在销毁结构时,您需要释放这些成员指向的存储空间。


特别是如果''name''和''defn' '是唯一指向

存储的指针,然后你必须释放()他们_before_在np上调用free(),

否则你会创建一个内存泄漏。


请注意,不需要调用free()中的转换为void *。


< blockquote> 10月25日晚上7:02,santosh< santosh .... @ gmail.comwrote:


Chad写道:


练习6-5


写一个函数undef,它将从
中删除一个名称和定义通过查找和安装维护
表。


代码:


unsigned hash(char * s);


void undef(char * s)

{

int h;

struct nlist * prev,* np;


prev = NULL;

h = hash(s);

for(np = hashtab [h]; np!= NULL; np = np-> next){

if(strcmp(s,np-> name)== 0)

break;

prev = np;

}


if(np!= NULL ){

if(prev == NULL)

hashtab [h] = np-> next;

else

prev-> next = np-> next;

free((void *)np-> name);

free((void *) np-> defn);

免费((void *)np);

}

}


问题是,为什么我必须释放()np-> name和np-> defn?

即,我怎么来不能免费()np?



因为他们的内存是单独分配的。

每次调用malloc()都需要调用free()。你构建了

struct np,其中三次调用malloc - 一次用于struct本身,

另外两次用来初始化它''的成员''name''和'' defn''指向

可用存储空间。因此,在销毁结构时,您需要释放这些成员指向的存储空间。


特别是如果''name''和''defn' '是唯一指向

存储的指针,然后你必须释放()他们_before_在np上调用free(),

否则你会创建一个内存泄漏。


请注意,不需要在调用free()时转换为void *。



问题来自The C Programming

Langauge第145页。由K& R.我想在尝试释放时绊倒了什么

结构是第145页的以下代码行。


在install()中,他们有以下代码行:


if((np-> defn = strdup(defn))== NULL)

返回NULL。


由于我仍然无法理解的原因,每当我看到这行代码时,我就会认为内存ISN已分配给np->定义。


10月25日晚上7:02,santosh< santosh .... @ gmail.comwrote:


Chad写道:


练习6-5


写一个functon undef,它将从通过查找和安装维护的

表中删除名称和定义。


代码:


unsigned hash(char * s);


void undef(char * s)

{

int h;

struct nlist * prev,* np;


prev = NULL;

h = hash(s);

for(np = hashtab [h]; np!= NULL; np = np-> next){

if(strcmp(s,np-> name)== 0)

break;

prev = np;

}


if(np!= NULL ){

if(prev == NULL)

hashtab [h] = np-> next;

else

prev-> next = np-> next;

free((void *)np-> name);

free((void *) np-> defn);

免费((void *)np);

}

}


问题是,为什么我必须释放()np-> name和np-> defn?

即,我怎么来不能免费()np?



因为他们的内存是单独分配的。

每次调用malloc()都需要调用free()。你构建了

struct np,其中三次调用malloc - 一次用于struct本身,

另外两次用来初始化它''的成员''name''和'' defn''指向

可用存储空间。因此,在销毁结构时,您需要释放这些成员指向的存储空间。


特别是如果''name''和''defn' '是唯一指向

存储的指针,然后你必须释放()他们_before_在np上调用free(),

否则你会创建一个内存泄漏。


请注意,不需要在调用free()时转换为void *。



问题来自The C Programming

Langauge第145页。由K& R(第二版)。我想是什么让我绊倒了

当试图释放结构时,下面的代码行是

第145页。


在install()中,它们具有以下代码行:


if((np-> defn = strdup(defn))== NULL)

返回NULL;


由于我仍然无法理解的原因,每次看到这行代码时,我都会继续认为内存ISN''T分配给np-> defn。


Excercise 6-5

Write a functon undef that will remove a name and defintion from the
table maintained by lookup and install.

Code:

unsigned hash(char *s);

void undef(char *s)
{
int h;
struct nlist *prev, *np;

prev = NULL;
h = hash(s);
for(np = hashtab[h]; np!=NULL; np = np->next){
if(strcmp(s, np->name) == 0)
break;
prev = np;
}

if(np !=NULL) {
if(prev == NULL)
hashtab[h] = np->next;
else
prev->next = np->next;
free((void *) np->name);
free((void *) np->defn);
free((void *) np);
}
}

The question is, how come I have to free() both np->name and np->defn?
Ie, how come I can''t just free() np?

解决方案

Chad wrote:

Excercise 6-5

Write a functon undef that will remove a name and defintion from the
table maintained by lookup and install.

Code:

unsigned hash(char *s);

void undef(char *s)
{
int h;
struct nlist *prev, *np;

prev = NULL;
h = hash(s);
for(np = hashtab[h]; np!=NULL; np = np->next){
if(strcmp(s, np->name) == 0)
break;
prev = np;
}

if(np !=NULL) {
if(prev == NULL)
hashtab[h] = np->next;
else
prev->next = np->next;
free((void *) np->name);
free((void *) np->defn);
free((void *) np);
}
}

The question is, how come I have to free() both np->name and np->defn?
Ie, how come I can''t just free() np?

Because memory for them was allocated separately.
You need a call to free() for every call to malloc(). You construct the
struct np with three calls to malloc - one for the struct itself and
two more to initialise it''s members ''name'' and ''defn'' to point to
usable storage. Thus when destroying the struct you need to deallocate
storage pointed to by these members too.

In particular if ''name'' and ''defn'' are the only pointers that point to
their storage, then you must free() them _before_ calling free() on np,
or else you''ll create a memory leak.

Note that the casts to void * within the calls to free() are not needed.


On Oct 25, 7:02 pm, santosh <santosh....@gmail.comwrote:

Chad wrote:

Excercise 6-5

Write a functon undef that will remove a name and defintion from the
table maintained by lookup and install.

Code:

unsigned hash(char *s);

void undef(char *s)
{
int h;
struct nlist *prev, *np;

prev = NULL;
h = hash(s);
for(np = hashtab[h]; np!=NULL; np = np->next){
if(strcmp(s, np->name) == 0)
break;
prev = np;
}

if(np !=NULL) {
if(prev == NULL)
hashtab[h] = np->next;
else
prev->next = np->next;
free((void *) np->name);
free((void *) np->defn);
free((void *) np);
}
}

The question is, how come I have to free() both np->name and np->defn?
Ie, how come I can''t just free() np?


Because memory for them was allocated separately.
You need a call to free() for every call to malloc(). You construct the
struct np with three calls to malloc - one for the struct itself and
two more to initialise it''s members ''name'' and ''defn'' to point to
usable storage. Thus when destroying the struct you need to deallocate
storage pointed to by these members too.

In particular if ''name'' and ''defn'' are the only pointers that point to
their storage, then you must free() them _before_ calling free() on np,
or else you''ll create a memory leak.

Note that the casts to void * within the calls to free() are not needed.


The question was taken from page 145 in the "The C Programming
Langauge" by K & R. I think what is tripping me up when trying to free
the structure is the following line of code on page 145.

In install(), they have the following line of code:

if((np->defn = strdup(defn)) == NULL)
return NULL.

For reasons that still elude me, every time I see this line of code, I
keep thinking that memory ISN''T allocated for np->defn.


On Oct 25, 7:02 pm, santosh <santosh....@gmail.comwrote:

Chad wrote:

Excercise 6-5

Write a functon undef that will remove a name and defintion from the
table maintained by lookup and install.

Code:

unsigned hash(char *s);

void undef(char *s)
{
int h;
struct nlist *prev, *np;

prev = NULL;
h = hash(s);
for(np = hashtab[h]; np!=NULL; np = np->next){
if(strcmp(s, np->name) == 0)
break;
prev = np;
}

if(np !=NULL) {
if(prev == NULL)
hashtab[h] = np->next;
else
prev->next = np->next;
free((void *) np->name);
free((void *) np->defn);
free((void *) np);
}
}

The question is, how come I have to free() both np->name and np->defn?
Ie, how come I can''t just free() np?


Because memory for them was allocated separately.
You need a call to free() for every call to malloc(). You construct the
struct np with three calls to malloc - one for the struct itself and
two more to initialise it''s members ''name'' and ''defn'' to point to
usable storage. Thus when destroying the struct you need to deallocate
storage pointed to by these members too.

In particular if ''name'' and ''defn'' are the only pointers that point to
their storage, then you must free() them _before_ calling free() on np,
or else you''ll create a memory leak.

Note that the casts to void * within the calls to free() are not needed.


The question was taken from page 145 in the "The C Programming
Langauge" by K & R (Second Edition). I think what is tripping me up
when trying to free the structure is the following line of code on
page 145.

In install(), they have the following line of code:

if((np->defn = strdup(defn)) == NULL)
return NULL;

For reasons that still elude me, every time I see this line of code, I
keep thinking that memory ISN''T allocated for np->defn.


这篇关于关于free()哈希表的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆