我正试图通过修改标准密钥类型,结构密钥示例来使用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 %d\n", p->key.a, p->key.b);
HASH_ITER(hh, records, p, tmp) {
HASH_DEL(records, p);
free(p);
}
return 0;
}
答案 0 :(得分:0)
我有个笨蛋。
从示例
record_t l, *p, *r, *tmp, *records = NULL;
将仅初始化记录为空。由于UTHash要求将“ head”(在本例中为记录)初始化为null,因此此示例有效。在我的示例中,头是“ stateHead”,并且未初始化为null。由于某种原因,我决定
t_State *state, *stateHead, *toFind = NULL;
将所有这些指针设置为null(不是C的工作方式)。