【发布时间】:2013-10-14 21:12:14
【问题描述】:
我对 C 很陌生。当我在 Valgrind 下为哈希表运行以下代码时:
table *insertObject (table *h, int pref, char ch)
{
struct node x;
int i;
if (ch < 0)
{
ch=256-ch;
}
x.chr=ch;
x.pref=pref;
i = hash(pref, ch, h->size);
while (h->hash[i].pref!=0)
{
i++;
}
h->hash[i]=x;
h->size++;
return h;
}
我收到以下错误:
==9243==
==9243== Process terminating with default action of signal 11 (SIGSEGV)
==9243== Bad permissions for mapped region at address 0x6018A4
==9243== at 0x4009CD: insertObject (encode.c:119)
==9243== by 0x4008E3: main (encode.c:55)
第119行是行
h->hash[i]=x;
有趣的是,当我通过调试器运行整个代码时,它 90% 的时间都可以正常工作。但是,对于某些特殊情况,代码段错误,调试器告诉我这也是罪魁祸首。怎么了?
【问题讨论】:
-
这一点都不好笑。欢迎编程;)
-
从表中删除对象时,您确定始终正确地清理哈希表条目吗?