【发布时间】:2015-11-13 02:00:31
【问题描述】:
我正在尝试编译一个示例程序以使用 lhash。我看不到关于 lhash 的好教程。所以,我理解 lhash 的唯一方法是使用 lhash linux 手册页。这是我正在尝试制作的示例。但是,我在执行 lh_insert 时发生了崩溃。我不知道为什么会这样。
/** In order to compile this program do the following **/
/** gcc lhastEx.c -lcrypto -o lhastEx.out **/
/** Install the openssl dev library on ubuntu by -- sudo apt-get install libssl-dev **/
/*** This is needed for library hash -- basically open ssl ones **/
#include <openssl/lhash.h>
/** Hash table -- just like maps in C++ i.e. QMAP -- it needs a key and the value **/
/*I have got a prints to check the flow */
#define __DBG (1)
static void dbgMsg(const char *msg)
{
#if __DBG
printf("%s",msg);
#endif
}
static int cmpFunc(const void *src, const void *dest)
{
dbgMsg("cmpFunc called..\r\n");
const int *obj1 = src;
const int *obj2 = dest;
return memcmp(obj1, obj2, sizeof(int));
}
static unsigned long keyHash(const void *entry)
{
unsigned long int hash = 0;
const int *val = entry;
dbgMsg("keyHash method invoked\r\n");
hash |= *(val);
return hash;
}
int main(int argc, char *argv[])
{
int *hashKey2 = malloc(sizeof(int));
int *hashKey3 = malloc(sizeof(int));
int *hashKey1 = malloc(sizeof(int));
*hashKey1 = 10;
*hashKey2 = 20;
*hashKey3 = 30;
/* we can make a function to generate this key unique **/
/** Ideally, this 1 should be a unique hash value **/
/************** Created the hash table now -- I see this as equivalent to the map in C ++ or QtMap **/
LHASH_OF(int) *hashtable = lh_new(keyHash, cmpFunc);
/*** add a new entry now **/
lh_insert(hashtable, hashKey2);
return 0;
}
【问题讨论】: