hsearch_r覆盖哈希表 [英] hsearch_r overwrites hash table

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

问题描述

我想看看我是否可以把一个新的功能到 inotifywait 了在观看阶段跟踪他们也没有收到他们的任何活动目录的列表,它退出之前,它打印的清单。
截至目前,inotifywait能够显示上接收到事件目录。我正在寻找的是目录的列表没有收到任何事件。

I'm trying to see if I can put a new feature into inotifywait that during the watching phase tracks the list of directories that did not receive any event on them, and before it quits, it prints the list. As of now, inotifywait is capable of showing on directories that received an event. What I'm looking for is for a list of directories that did not receive any event.

要做到这一点,我采取以下做法。

To achieve this, I'm taking the following approach.


  1. 当inotifywait拼劲手表,生成所有目录的数组,并使用hsearch_r与主要为目录名的哈希表,使用功能哈德,和值作为数组元素的索引此目录刚创建。见code这里

  2. 当甚至发生在一个给定的目录,然后,我去在步骤1中创建的哈希表, H找到出对应于该目录名(其值是的数组元素的索引),并且阵列为NULL于该索引的设置元素。请参见哈希键
  3. 在我退出inotifywait,我将打印阵列中所有的非NULL的元素。这本质上是没有收到inotifywait任何情况下的目录列表。

  1. While inotifywait is placing watches, generate an array of all the directories, and a hashtable using hsearch_r with key as directory name, using the function hadd, and value as the index of the array element just created for this directory.See code here
  2. When an even occurs on a given directory, then, I go to the hash table created in step1, hfind out the value corresponding to this directory name(which is the index of the array element), and set the element at that index in the array to NULL. See search for hash key
  3. Before I exit inotifywait, I will print all the elements in the array that are not NULL. This is essentially the list of directories that did not receive any EVENT from inotifywait.

我看到的问题是,哈德成功,接下来的语句 H找到的元素只是插入的作品。当hsearch_r FIND当偶数时,第2步上面的失败。看看有多少元素在哈希表在那里,我硬$ C $光盘 H找到对我做了之后目录名称哈德。这通过仅在硬codeD目录才刚刚插入。随后,下一个搜索失败,因为在哈希表似乎已被覆盖与下一个元素。

The problem I'm seeing is, hadd succeeds, and the next statement to hfind for the element just inserted works. When the hsearch_r FIND when an even occurs, step 2 above fails. To see how many elements are there in the hashtable, I hardcoded a dir name in hfind on after I did a hadd. This passes only when the hardcoded directory is just inserted. Subsequently, the next search fails, because, the hashtable seem to have been overwritten with the next element.

我希望有人看到我哪里错了。
我读的问题 hsearch_r 并发布一个简短的讨论后,有一个新的。

I'm hoping that somebody sees where I went wrong. I read the question hsearch_r and posting a new one after a brief discussion there.

推荐答案

我没有看过深入的code,但 hsearch 要求钥匙一生至少只要 htables 的,因为它只是存储条目,因为它是不使指针的深拷贝。

I haven't looked at your code in depth, but hsearch requires that the keys' lifetime is at least as long as the htables's, because it just stores the entry as it is without making a deep copy of the pointers.

您使用一个临时指针来保存当前的关键;您 aprintf 来和免费这个指针频繁。在第二查询的时间,该句柄不再有效。 (你可以运行Valgrind的的code找到这样的内存访问错误。)

You use a temporary pointer to store the current key; you aprintf to and free this pointer frequently. At the time of the second look-up, the handle is no longer valid. (You could run the code in Valgrind to find such memory access errors.)

您也许可以逃脱复制字符串:

You could probably get away with duplicating the string:

hadd(&tab, strdup(next_file), dir_count);

但是这并不是一个妥善的解决办法要么,因为当你破坏了 htable 的钥匙内存泄漏。

男人hsearch

hdestroy() hdestroy_r()功能不释放缓冲区指向哈希表项的键和数据元素。

The hdestroy() and hdestroy_r() functions do not free the buffers pointed to by the key and data elements of the hash table entries.

A htable 没有自己的数据。它只是提供了一个快速查找对已经存在的数据,你必须自己管理。 (它有其他限制:条目的最大数目必须事先已知的,并且项不能被删除)

A htable doesn't own the data. It just provides a fast look-up for already existing data, which you must manage yourself. (It has other limitations: The maximum number of entries must be known beforehand and items can't be deleted.)

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

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