我试图通过修改Standard Key Types,Struct Keys示例来使用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;
}请注意,在main中,我注释掉了变量d。按照下面的方式运行代码没有任何问题,但是当我取消注释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 %d\n", p->key.a, p->key.b);
HASH_ITER(hh, records, p, tmp) {
HASH_DEL(records, p);
free(p);
}
return 0;
}发布于 2018-12-06 08:53:49
我犯了个愚蠢的错误。
从示例中
record_t l, *p, *r, *tmp, *records = NULL;将只将记录初始化为空。由于UTHash需要将"head“(在本例中为记录)初始化为null,因此此示例可以工作。在我的例子中,头部是"stateHead“,并且没有初始化为null。出于某种原因,我决定
t_State *state, *stateHead, *toFind = NULL;将所有这些指针设置为null (不是C的工作方式)。
https://stackoverflow.com/questions/53642712
复制相似问题