我对 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% 的时间它都能正常工作。但是,对于某些特殊情况,代码段错误,调试器告诉我这也是罪魁祸首。怎么了?