我想做一些基于dict的基本的put/get测试。但是,在执行dictAdd时,会引发错误。
码
// 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)
编译命令
gcc dict_test.c dict.c zmalloc.c siphash.c -o dict_test我发现了执行dictIsRehashing(d)时抛出的错误,这是一个宏(#define dictIsRehashing(d) ((d)->rehashidx != -1))。
所以我尝试直接打印d->rehashidx。但我还是犯了同样的错误。
printf("%ld \n", d->rehashidx);
printf("%s", ((d)->rehashidx) == -1L ? "False" : "True");输出量
Created: OK
-1
fish: './dict_test' terminated by signal SIGSEGV (Address boundary error)也许这是一个基本的c问题。如有任何提示将不胜感激。
复制的步骤
下载Redis的源代码:). src.目录下的
发布于 2019-12-12 07:05:38
执行部分的解决方案。
问题是当使用dict时,需要单独插入散列函数,我认为它有一个默认的哈希函数。通过定义哈希函数,解决了崩溃问题。
#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;
}输出
Created: OK
hello
Value: worldhttps://stackoverflow.com/questions/58851956
复制相似问题