【发布时间】:2011-11-11 02:12:48
【问题描述】:
我似乎在 strcmp 函数的某个地方遇到了分段错误。 我对 C 还是很陌生,我不明白为什么它会给我这个错误。
int linear_probe(htable h, char *item, int k){
int p;
int step = 1;
do {
p = (k + step++) % h->capacity;
}while(h->keys[p] != NULL && strcmp(h->keys[p], item) != 0);
return p;
}
gdb:
Program received signal SIGSEGV, Segmentation fault.
0x0000003a8e331856 in __strcmp_ssse3 () from /lib64/libc.so.6
(gdb) frame 1
#1 0x0000000000400ea6 in linear_probe (h=0x603010, item=0x7fffffffde00 "ksjojf", k=-1122175319) at htable.c:52
编辑:插入代码和htable结构
int htable_insert(htable h, char *item){
unsigned int k = htable_word_to_int(item);
int p = k % h->capacity;
if(NULL == h->keys[p]){
h->keys[p] = (char *)malloc(strlen(item)+1);
strcpy(h->keys[p], item);
h->freqs[p] = 1;
h->num_keys++;
return 1;
}
if(strcmp(h->keys[p], item) == 0){
return ++h->freqs[p];
}
if(h->num_keys == h->capacity){
return 0;
}
if(h->method == LINEAR_P) p = linear_probe(h, item, k);
else p = double_hash(h, item, k);
if(NULL == h->keys[p]){
h->keys[p] = (char *)malloc(strlen(item)+1);
strcpy(h->keys[p], item);
h->freqs[p] = 1;
h->num_keys++;
return 1;
}else if(strcmp(h->keys[p], item) == 0){
return ++h->freqs[p];
}
return 0;
}
struct htablerec{
int num_keys;
int capacity;
int *stats;
char **keys;
int *freqs;
hashing_t method;
};
谢谢
编辑:valgrind - 我输入随机值添加到表中
sdkgj
fgijdfh
dfkgjgg
jdf
kdjfg
==25643== Conditional jump or move depends on uninitialised value(s)
==25643== at 0x40107E: htable_insert (htable.c:87)
==25643== by 0x400AB7: main (main.c:75)
==25643==
fdkjb
kjdfg
kdfg
nfdg
lkdfg
oijfd
kjsf
vmf
kjdf
kjsfg
fjgd
fgkjfg
==25643== Invalid read of size 8
==25643== at 0x400E0E: linear_probe (htable.c:51)
==25643== by 0x401095: htable_insert (htable.c:87)
==25643== by 0x400AB7: main (main.c:75)
==25643== Address 0x4c342a0 is not stack'd, malloc'd or (recently) free'd
==25643==
==25643== Invalid read of size 8
==25643== at 0x400E2B: linear_probe (htable.c:51)
==25643== by 0x401095: htable_insert (htable.c:87)
==25643== by 0x400AB7: main (main.c:75)
==25643== Address 0x4c342a0 is not stack'd, malloc'd or (recently) free'd
==25643==
==25643== Invalid read of size 1
==25643== at 0x4A06C51: strcmp (mc_replace_strmem.c:426)
==25643== by 0x400E3C: linear_probe (htable.c:51)
==25643== by 0x401095: htable_insert (htable.c:87)
==25643== by 0x400AB7: main (main.c:75)
==25643== Address 0x210 is not stack'd, malloc'd or (recently) free'd
==25643==
==25643==
==25643== Process terminating with default action of signal 11 (SIGSEGV)
==25643== Access not within mapped region at address 0x210
==25643== at 0x4A06C51: strcmp (mc_replace_strmem.c:426)
==25643== by 0x400E3C: linear_probe (htable.c:51)
==25643== by 0x401095: htable_insert (htable.c:87)
==25643== by 0x400AB7: main (main.c:75)
==25643== If you believe this happened as a result of a stack
==25643== overflow in your program's main thread (unlikely but
==25643== possible), you can try to increase the size of the
==25643== main thread stack using the --main-stacksize= flag.
==25643== The main thread stack size used in this run was 8388608.
==25643==
==25643== HEAP SUMMARY:
==25643== in use at exit: 1,982 bytes in 28 blocks
==25643== total heap usage: 28 allocs, 0 frees, 1,982 bytes allocated
==25643==
==25643== LEAK SUMMARY:
==25643== definitely lost: 0 bytes in 0 blocks
==25643== indirectly lost: 0 bytes in 0 blocks
==25643== possibly lost: 0 bytes in 0 blocks
==25643== still reachable: 1,982 bytes in 28 blocks
==25643== suppressed: 0 bytes in 0 blocks
==25643== Rerun with --leak-check=full to see details of leaked memory
==25643==
==25643== For counts of detected and suppressed errors, rerun with: -v
==25643== Use --track-origins=yes to see where uninitialised values come from
==25643== ERROR SUMMARY: 7 errors from 4 contexts (suppressed: 6 from 6)
Segmentation fault (core dumped)
static unsigned int htable_word_to_int(char *word){
unsigned int result = 0;
while(*word != '\0'){
result = (*word++ + 31 * result);
}
return result;
}
【问题讨论】:
-
是否成功通过了打印语句?你确定你的字符串是空终止的吗?
-
h->keys[p]或item是无效指针。这将有助于在调试器下运行它,这样您就可以在故障点打印这些值。 -
您发布了 GDB sn-p,但这还不够。一个建议是转到包含您的代码的堆栈框架并开始四处寻找。
-
@asveikau 很抱歉刚接触 C,但我该怎么做呢?
-
#asveikau:我想我明白了。检查 gdb sn -p 下的编辑。但是即使我的 k 是负数,由于 mod 的原因,p 不能保证正值吗?
标签: c segmentation-fault hashtable