首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在C中,lhash在lh_insert上崩溃?

在C中,lhash在lh_insert上崩溃?
EN

Stack Overflow用户
提问于 2015-11-13 02:00:31
回答 2查看 744关注 0票数 2

我正在尝试编译一个示例程序来使用lhash。我看不出一本好的关于拉什的教程。因此,我理解l散列的唯一方法是使用linux手册页。下面是我想要做的例子。但是,在执行lh_insert时,我会崩溃。我不知道为什么会发生这种事。

代码语言:javascript
复制
/** 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;

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-11-13 03:15:54

LHASH可以使用(在某种程度上)类似于堆栈API。此外,还有OpenSSL源代码可以帮助您了解它。例如,请参见crypto/err/err.c,它使用了ERR_STRING_DATA的哈希表,而宏定义则参见lhash.hsafestack.h

最安全的方法是为表定义强类型函数。在这种情况下,一个关键方面是哈希表中的元素必须是结构类型。有关如何使用带有单个int字段的结构的整数哈希表的示例,请参见下面的代码。

代码语言:javascript
复制
#include <openssl/lhash.h>
#include <string.h>

#define __DBG (1)

static void dbgMsg(const char *msg)
{
#if __DBG
    printf("%s", msg);
#endif 
}

typedef struct int_value_st {
    int value;
} INT_VALUE;

static int int_value_cmp(const INT_VALUE *a, const INT_VALUE *b)
{
    dbgMsg("cmpFunc called..\r\n");
    return a->value - b->value;
}
static IMPLEMENT_LHASH_COMP_FN(int_value, INT_VALUE);

static unsigned long int_value_hash(const INT_VALUE *entry)
{
    unsigned long int hash = 0;
    dbgMsg("keyHash method invoked\r\n");

    hash |= entry->value;

    return hash;
}
static IMPLEMENT_LHASH_HASH_FN(int_value, INT_VALUE);

/* See stack/safestack.h for a complete list of the possible #defines */
#define lh_INT_VALUE_new() LHM_lh_new(INT_VALUE,int_value)
#define lh_INT_VALUE_insert(lh,inst) LHM_lh_insert(INT_VALUE,lh,inst)
#define lh_INT_VALUE_retrieve(lh,inst) LHM_lh_retrieve(INT_VALUE,lh,inst)
#define lh_INT_VALUE_delete(lh,inst) LHM_lh_delete(INT_VALUE,lh,inst)
#define lh_INT_VALUE_free(lh) LHM_lh_free(INT_VALUE,lh)

int LHashTest(void)
{
    DECLARE_LHASH_OF(INT_VALUE);

    INT_VALUE *hashKey1 = OPENSSL_malloc(sizeof(*hashKey1));
    INT_VALUE *hashKey2 = OPENSSL_malloc(sizeof(*hashKey2));
    INT_VALUE *hashKey3 = OPENSSL_malloc(sizeof(*hashKey3));
    INT_VALUE *hashKeyFound = NULL;

    hashKey1->value = 10;
    hashKey2->value = 20;
    hashKey3->value = 30;

    LHASH_OF(INT_VALUE) *hashtable = NULL;
    hashtable = lh_INT_VALUE_new();
    lh_INT_VALUE_insert(hashtable, hashKey1);
    lh_INT_VALUE_insert(hashtable, hashKey2);
    lh_INT_VALUE_insert(hashtable, hashKey3);

    /* Should find result */
    hashKeyFound = lh_INT_VALUE_retrieve(hashtable, hashKey2);
    lh_INT_VALUE_delete(hashtable, hashKeyFound);
    /* Should not find result */
    hashKeyFound = lh_INT_VALUE_retrieve(hashtable, hashKey2);

    /* OPENSSL_free()s all elements */
    lh_INT_VALUE_free(hashtable);

    return 1;
}

顺便说一句,你好像忽略了一些编译器警告.这通常不是一件好事。

票数 1
EN

Stack Overflow用户

发布于 2015-11-13 03:21:49

我发现这是由于open的向后不兼容版本造成的。这是更正-

代码语言:javascript
复制
  _LHASH *hashtable = lh_new(keyHash, cmpFunc);

只是效果很好。下面是完整的代码。这可能会帮助一些新的人尝试他的手在拉什。不过,我认为C++提供了比C更简洁的散列方式。我不喜欢这个库。然而,它被用于旧的项目。

代码语言:javascript
复制
/** 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 inputBucket = 0;
   int *hashKey2 = malloc(sizeof(int));

   int *hashKey3 = malloc(sizeof(int));

   int *hashKey1 = malloc(sizeof(int));

   *hashKey1 = 10;
   *hashKey2 = 20;
   *hashKey3 = 30;

   int *ptrInputBucket = &inputBucket;

   /* 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 *hashtable = lh_new(keyHash, cmpFunc);
   /*** add a new entry now **/

   lh_insert(hashtable, hashKey2);
   lh_insert(hashtable, hashKey3);

   /** now retrieve the data **/
   ptrInputBucket = lh_retrieve(hashtable, hashKey2);

   if( ptrInputBucket != NULL )
    {
       printf("The value retrieve from Hash Table is %d\r\n", *ptrInputBucket);
    }
  return 0;

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33684774

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档