Reputation: 33
I'm pretty new to C. When I run the following code for a hash table under 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;
}
I get the following error:
==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)
Line 119 is the line
h->hash[i]=x;
The funny thing is, when I run the whole code through a debugger, it works fine 90% of the time. However, for some special cases, the code segfaults, and the debugger tells me this is also the culprit. What's wrong?
Upvotes: 2
Views: 11498
Reputation: 7912
The error is due to an incorrect memory access, basically your application is trying to access a memory area which is not mapped in its memory space.
Quite likely the value of i
exceeds the limits of the hash array. I cannot be more precise because I do not how the hash function works and what perf
stands for.
However, you should verify the value of i
with a debugger, in the 10% of case where the application does not work.
P.S. a program should work fine 100% of time.
Upvotes: 1