我正试图通过修改标准密钥类型、结构密钥示例来使用UTHash,如下链接所示:
https://troydhanson.github.io/uthash/userguide.html#_structure_keys
这是我修改后的代码(精简后显示我将问题隔离到的位置(
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "uthash.h"
typedef struct StateKey
{
// array of bools that gives the instances that are present.
bool *instancesAtNode_BoolArray;
} t_StateKey;
typedef struct State
{
// State Key.
t_StateKey stateKey_StateKey;
// probability of being in the given state
double p;
// UTHash handle array used for hashing
UT_hash_handle hh;
} t_State;
int main(int argc, char *argv[]) {
double a = .80;
double b = .2;
double c = .1;
//double d = .7;
t_State *state, *stateHead, *toFind = NULL;
state = (t_State *) malloc(sizeof(t_State));
memset(state, 0, sizeof(t_State));
state->stateKey_StateKey.instancesAtNode_BoolArray = NULL;
state->p = 1;
HASH_ADD(hh, stateHead, stateKey_StateKey, sizeof(t_StateKey), state);
return 0;
}
请注意,我主要注释掉了变量d。按照下面的方式运行代码没有问题,但当我取消注释代码时,会出现分段错误。对我来说,这表明存在某种越界错误,只有当代码具有特定的大小/组织时,操作系统才会发现(这就是为什么注释掉一个看似无关的变量可以防止错误的原因(。
我不知道自己做错了什么,因为据我所知,我一直在效仿这个例子。看看Valgrind,我得到了以下
==94553== Conditional jump or move depends on uninitialised value(s)
==94553== at 0x10000195F: main (testNewMcUniverseMain.c:40)
==94553== Uninitialised value was created by a stack allocation
==94553== at 0x7FFF5FC01036: _dyld_start (in /usr/lib/dyld)
==94553==
==94553== Use of uninitialised value of size 8
==94553== at 0x100001A9F: main (testNewMcUniverseMain.c:40)
==94553== Uninitialised value was created by a stack allocation
==94553== at 0x7FFF5FC01036: _dyld_start (in /usr/lib/dyld)
==94553==
==94553== Use of uninitialised value of size 8
==94553== at 0x100001ABF: main (testNewMcUniverseMain.c:40)
==94553== Uninitialised value was created by a stack allocation
==94553== at 0x7FFF5FC01036: _dyld_start (in /usr/lib/dyld)
==94553==
==94553== Use of uninitialised value of size 8
==94553== at 0x100001ACB: main (testNewMcUniverseMain.c:40)
==94553== Uninitialised value was created by a stack allocation
==94553== at 0x7FFF5FC01036: _dyld_start (in /usr/lib/dyld)
==94553==
==94553== Use of uninitialised value of size 8
==94553== at 0x100001AE6: main (testNewMcUniverseMain.c:40)
==94553== Uninitialised value was created by a stack allocation
==94553== at 0x7FFF5FC01036: _dyld_start (in /usr/lib/dyld)
==94553==
==94553== Invalid write of size 8
==94553== at 0x100001AEE: main (testNewMcUniverseMain.c:40)
==94553== Address 0x5400313d524f4c5f is not stack'd, malloc'd or
(recently) free'd
==94553==
==94553== Signal 11 being dropped from thread 0's queue
==94553== Signal 11 being dropped from thread 0's queue
==94553== Signal 11 being dropped from thread 0's queue
==94553== Signal 11 being dropped from thread 0's queue
(Repeats this line forever, I had to kill the terminal)
我是做错了什么,还是这是UTHash的问题?如果是UTHash,那么我可以为C(而不是C++(使用的另一个哈希表库是什么?
为了简单起见,我从下面引用的源代码中复制了UTHash示例代码
#include <stdlib.h>
#include <stdio.h>
#include "uthash.h"
typedef struct {
char a;
int b;
} record_key_t;
typedef struct {
record_key_t key;
/* ... other data ... */
UT_hash_handle hh;
} record_t;
int main(int argc, char *argv[]) {
record_t l, *p, *r, *tmp, *records = NULL;
r = (record_t *)malloc(sizeof *r);
memset(r, 0, sizeof *r);
r->key.a = 'a';
r->key.b = 1;
HASH_ADD(hh, records, key, sizeof(record_key_t), r);
memset(&l, 0, sizeof(record_t));
l.key.a = 'a';
l.key.b = 1;
HASH_FIND(hh, records, &l.key, sizeof(record_key_t), p);
if (p) printf("found %c %dn", p->key.a, p->key.b);
HASH_ITER(hh, records, p, tmp) {
HASH_DEL(records, p);
free(p);
}
return 0;
}
我有一个愚蠢的。
从的例子
record_t l, *p, *r, *tmp, *records = NULL;
将ONLY记录初始化为null。由于UTHash要求将"head"(在本例中为记录(初始化为null,因此此示例有效。在我的例子中,head是"stateHead",并且没有初始化为null。出于某种原因,我决定
t_State *state, *stateHead, *toFind = NULL;
将所有这些指针设置为null(而不是C的工作方式(。