calloc和哈希表 [英] calloc and hash tables

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

问题描述



我在openvms系统上的哈希表程序中使用了一个calloc。

calloc用于声明哈希表

char ** pHashes;

pHashes = calloc(hash_size,sizeof(char *)) ; // hash_size = 101


然后在半满时我会做以下事情


char ** newHashes;

if(newHashes)

free(newHashes);


newHashes = calloc(new_hash_size,sizeof(char *)); // new_hash_size =

hash_size * 2

//将项目复制到newHashes

免费(pHashes)

pHashes = newHashes;


现在一切正常,除非hash_size为101 ....我试过

hash_size = 1009并且它工作正常,基本上如果它没有进入

newHashes calloc,它看起来工作得很好......而另一个奇怪的事情是

它在哈希表程序中没有崩溃,而是稍后它就崩溃了

在fopen中崩溃了。是的,我检查了fopen的返回状态,而且它还没有达到
。在fopen里面它崩溃了......

我猜第二个calloc正在破坏内存,后来会导致内存失败导致
fopen失败。关于如何进行调试的任何帮助

这将非常感谢。

谢谢。

Hi
I am using a calloc in a hash table program on an openvms system. The
calloc is used to declare the hash table
char **pHashes;
pHashes = calloc(hash_size,sizeof(char *)); //hash_size = 101

and then later on when it is half full i do the following

char **newHashes;
if(newHashes)
free(newHashes);

newHashes = calloc(new_hash_size,sizeof(char *)); //new_hash_size =
hash_size * 2
// copy over the items to newHashes
free(pHashes)
pHashes = newHashes;

Now all works well except when hash_size is 101....I tried
hash_size=1009 and it works fine, basically if it doesnt go into the
newHashes calloc, it seems to work fine...and another weird thing is
that it doesnt crash in the hashtable program, rather later on it
crashes in an fopen. Yes I checked the return status of fopen and it
doesnt even get to that. Inside fopen it crashes...
I guess that second calloc is corrupting memory which later on causes
fopen to fail. Any help regarding how I should proceed with debugging
this would be greatly appreciated.
Thanks.

推荐答案

" ababeel" < fa ********* @ gmail.comwrites:
"ababeel" <fa*********@gmail.comwrites:

char ** newHashes;

if(newHashes)

免费(newHashes);
char **newHashes;
if(newHashes)
free(newHashes);



这是一个错误。你不能使用你没有初始化的
变量的值。你当然不能释放一个具有

不确定值的指针。


也许你的代码有其他错误,但那个很明显。

-

我看到它的方式,一个不同意我的聪明人是

可能是我最重要的人我将在任何给定的

日内进行互动。

--Billy Chambless

That''s a bug. You can''t use the value of a variable you haven''t
initialized. You certainly can''t free a pointer that has an
indeterminate value.

Perhaps your code has other bugs, but that one''s obvious.
--
"The way I see it, an intelligent person who disagrees with me is
probably the most important person I''ll interact with on any given
day."
--Billy Chambless


是......

我改变了,删除了if(newHashes)位...仍然相同

虽然...


3月下午1:12:17,Ben Pfaff< b ... @ cs.stanford.eduwrote:
Yes...
I changed that, removed the if(newHashes) bit...still the same
though...

On Mar 1, 12:17 pm, Ben Pfaff <b...@cs.stanford.eduwrote:

" ababeel" < farooq.o ... @ gmail.comwrites:
"ababeel" <farooq.o...@gmail.comwrites:

char ** newHashes;

if(newHashes)

免费(newHashes);
char **newHashes;
if(newHashes)
free(newHashes);



这是一个错误。你不能使用你没有初始化的
变量的值。你当然不能释放一个具有

不确定值的指针。


也许你的代码有其他错误,但那个很明显。

-

我看到它的方式,一个不同意我的聪明人是

可能是我最重要的人我将在任何给定的

日内互动。

--Billy Chambless


That''s a bug. You can''t use the value of a variable you haven''t
initialized. You certainly can''t free a pointer that has an
indeterminate value.

Perhaps your code has other bugs, but that one''s obvious.
--
"The way I see it, an intelligent person who disagrees with me is
probably the most important person I''ll interact with on any given
day."
--Billy Chambless



2月28日下午5:48,ababeel < farooq.o ... @ gmail.comwrote:
On Feb 28, 5:48 pm, "ababeel" <farooq.o...@gmail.comwrote:

是......

我改变了,删除了if(newHashes) )......仍然一样

虽然...


3月1日下午12:17,Ben Pfaff< b ... @ cs.stanford.eduwrote:
Yes...
I changed that, removed the if(newHashes) bit...still the same
though...

On Mar 1, 12:17 pm, Ben Pfaff <b...@cs.stanford.eduwrote:

" ababeel" < farooq.o ... @ gmail.comwrites:
"ababeel" <farooq.o...@gmail.comwrites:

char ** newHashes;

if(newHashes)

免费(newHashes);
char **newHashes;
if(newHashes)
free(newHashes);


这是一个错误。你不能使用你没有初始化的
变量的值。你肯定不能释放一个具有

不确定值的指针。
That''s a bug. You can''t use the value of a variable you haven''t
initialized. You certainly can''t free a pointer that has an
indeterminate value.


也许你的代码有其他错误,但那个很明显。

-

我看到它的方式,一个不同意我的聪明人是

可能是我将在任何给定的

日与之互动的最重要人物。"

--Billy Chambless-隐藏引用文字 -
Perhaps your code has other bugs, but that one''s obvious.
--
"The way I see it, an intelligent person who disagrees with me is
probably the most important person I''ll interact with on any given
day."
--Billy Chambless- Hide quoted text -



- 显示引用文字 -


- Show quoted text -



如果例程不是太长(最多几百行)那么

发布它。

我们猜不出你是什么没看到

满了的代码就错了。

If the routine is not too long (a couple hundred lines at most) then
post it.
We can''t guess what you are doing wrong without seeing the code in
full.


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

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