为什么调用 bsearch() 会使所呈现的程序崩溃?
我有一个名为“dict.txt”的未排序字典文件。 我已经设法将文件的单词放入数组中,并且我使用的 qsort() 似乎也工作得很好(也就是说,数组已排序)。
当我调用 bsearch() 时出现问题, 程序崩溃了,我的问题是:
为什么会发生这种情况?
我使用 gcc 进行编译,并且不使用任何类型的 IDE,因此我没有任何调试器,也不知道如何使用它(还)。
我很清楚这里提供的代码可能包含几个问题。
这是因为我对 c 很陌生,而且我的背景主要是 Java(尽管有相似之处,但这似乎是一个缺点,因为我太习惯 OO 而 c 显然不是 OO)。
任何建议将不胜感激。
int strcmp_mod(const void *p1, const void *p2) {
return strcmp(* (char * const *) p1, * (char * const *) p2);
}
int main(void) {
int size, i;
char **words;
char *pItem;
char *key = "fight";
char* buf = load_file("dict.txt"); if (buf == NULL) return 1;
size = count_words(buf);
words = (char**)malloc((size+1) * sizeof(char*));
for (i=0; i<size; i++) {
words[i] = (char*)malloc(80 * sizeof(char));
}
copy_words_to_lower(buf, words, size);
words[size] = '\0';
qsort(words, size, sizeof(char*), strcmp_mod);
for (i=0; i<size; i++) {
printf("%s\n", words[i]);
}
pItem = (char *) bsearch(key, words, size, sizeof(char*), strcmp_mod);
if (pItem!=NULL)
printf ("%s is in the array.\n", pItem);
else
printf ("%s is not in the array.\n", key);
return 0;
}
I have an unsorted dictionary file named "dict.txt".
I have managed to put the words of the file in an array and the qsort() I use also seems to be working just fine (That is, the array is sorted).
The problem arises when I call bsearch(),
the program crashes and my question is:
Why is this happening?
I use gcc to compile and don't use an IDE of any sort so I don't have any debugger nor do I know how to use one (yet).
I am quite aware that the code presented here might contain several problems.
That is because I am quite new to c and my background is mainly Java (which despite the similarities seems to be a drawback, because I am so used to OO and c is obviously not OO).
Any advice would be greatly appreciated.
int strcmp_mod(const void *p1, const void *p2) {
return strcmp(* (char * const *) p1, * (char * const *) p2);
}
int main(void) {
int size, i;
char **words;
char *pItem;
char *key = "fight";
char* buf = load_file("dict.txt"); if (buf == NULL) return 1;
size = count_words(buf);
words = (char**)malloc((size+1) * sizeof(char*));
for (i=0; i<size; i++) {
words[i] = (char*)malloc(80 * sizeof(char));
}
copy_words_to_lower(buf, words, size);
words[size] = '\0';
qsort(words, size, sizeof(char*), strcmp_mod);
for (i=0; i<size; i++) {
printf("%s\n", words[i]);
}
pItem = (char *) bsearch(key, words, size, sizeof(char*), strcmp_mod);
if (pItem!=NULL)
printf ("%s is in the array.\n", pItem);
else
printf ("%s is not in the array.\n", key);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试为
bsearch
提供key
的地址。Try giving
bsearch
the address ofkey
.为什么会发生这种情况?
您将
char*
作为key
参数传递给 bsearch,但您的比较器期望转换char*
的结果代码>char** 到 void*。解决这个问题后,下一个问题是 bsearch 的返回值是指向数组中匹配项的指针。所以再次强调
char**
不是char*
。任何建议都将不胜感激。
要么获得一个调试器,要么准备向您的代码添加大量日志记录。
此外,您的
words
数组的构造也略有偏差。事实上,它完成了工作,但可能是一个想法,为每个单词分配缓冲区,而不是在开始时分配所有相同的大小。谁知道是否有人会向您发送一个文件,其中的单词长度超过 80 个字符?当您可能想用空指针(NULL)来终止单词列表时,您可以用空字符('\0')来终止单词列表。 '\0' 实际上是有效的,因为它是 0 的另一种表达方式,并且 0 会转换为空指针。但这不是你的意思。并且该数组现在根本不需要以 null 结尾,因为此后每次使用它时都需要指定其长度size
。Why is this happening?
You're passing a
char*
as thekey
parameter to bsearch, but your comparator expects the result of casting achar**
to void*.Once you've fixed that, the next problem is that the return value from bsearch is a pointer to the matching item in the array. So again a
char**
not achar*
.Any advice would be greatly appreciated.
Either get a debugger, or else get ready to add lots of logging to your code.
Also the construction of your
words
array is slightly off. As it is it gets the job done, but it might be an idea to allocate the buffer for each word as you go, rather than all the same size at the start. Who knows if somebody will send you a file with a word in it more than 80 chars long? You terminate the list of words with a nul character, '\0', when you probably mean to terminate it with a null pointer, NULL. '\0' actually works, because it's another way of saying 0, and 0 converts to a null pointer. But it's not what you mean. And the array doesn't need to be null-terminated at all right now, because every time you use it after that you specify its length,size
.