首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >添加Redis的“命令”时,如何解决地址边界错误?

添加Redis的“命令”时,如何解决地址边界错误?
EN

Stack Overflow用户
提问于 2019-11-14 08:01:52
回答 1查看 85关注 0票数 0

我想做一些基于dict的基本的put/get测试。但是,在执行dictAdd时,会引发错误。

代码语言:javascript
复制
// dict_test.c
#include <stdio.h>
#include "dict.h"

int main(int argc, char *argv[]) {
    // create
    dictType hashDictType;
    dict *d = dictCreate(&hashDictType, NULL);
    printf("Created: %s\n", d == NULL ? "Failed" : "OK");
    // put
    char key[] = "hello";
    char value[] = "world";
    dictAdd(d, key, value);
    return 0;
}

错误消息

“./dict_test”由信号SIGSEGV (地址边界错误)终止

GCC版

Apple LLVM版本10.0.1 (clang-1001.0.46.4)

编译命令

代码语言:javascript
复制
gcc dict_test.c dict.c zmalloc.c siphash.c  -o dict_test

我发现了执行dictIsRehashing(d)时抛出的错误,这是一个宏(#define dictIsRehashing(d) ((d)->rehashidx != -1))。

所以我尝试直接打印d->rehashidx。但我还是犯了同样的错误。

代码语言:javascript
复制
printf("%ld \n", d->rehashidx);
printf("%s", ((d)->rehashidx) == -1L ? "False" : "True");

输出量

代码语言:javascript
复制
Created: OK
-1 
fish: './dict_test' terminated by signal SIGSEGV (Address boundary error)

也许这是一个基本的c问题。如有任何提示将不胜感激。

复制的步骤

下载Redis的源代码:). src.目录下的

  • 粘贴演示代码
EN

回答 1

Stack Overflow用户

发布于 2019-12-12 07:05:38

执行部分的解决方案。

问题是当使用dict时,需要单独插入散列函数,我认为它有一个默认的哈希函数。通过定义哈希函数,解决了崩溃问题。

代码语言:javascript
复制
#include <stdio.h>
#include <strings.h>
#include "dict.h"

// a hash function defined
uint64_t dictSdsCaseHash(const void *key) {
    // refer to "siphash.c"
    return dictGenCaseHashFunction((unsigned char*)key, (int) strlen((char*)key));
}

int main(int argc, char *argv[]) {
    // create
    dictType hashDictType;
    hashDictType.hashFunction = dictSdsCaseHash;
    dict *d = dictCreate(&hashDictType, NULL);
    printf("Created: %s\n", d == NULL ? "Failed" : "OK");
    // put
    char key[] = "hello";
    char value[] = "world";
    dictAdd(d, key, value);
    // get
    dictEntry *entry = dictFind(d, key);
    printf("Value: %s\n", entry->v);
    return 0;
}

输出

代码语言:javascript
复制
Created: OK
hello 
Value: world
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58851956

复制
相关文章

相似问题

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