帮助解决函数中的分段错误
我已经尝试解决这个搜索作业问题有一段时间了。我尝试使用我的代码首先搜索一个条目,如下所示:
int Compare(const void *a, const void *b);
void SortStudents(char *studentList[], size_t studentCount)
{
qsort(studentList, studentCount, sizeof(studentList[0]), Compare);
}
int Compare(const void *a, const void *b)
{
return (strcmp(*(char **)a, *(char **)b));
}
char *SearchList(char *key, char *list[], size_t num)
{
char **value = bsearch(&key, list, num, sizeof(list[0]), Compare);
return (value == 0 ? 0 : *value);
}
/*Determines which registrants did not attend the first meeting by searching for registrants
that are not in attendees set. */
void DisplayClassStatus(
const char *registrants[], size_t registrantCount,
const char *attendees[], size_t attendeeCount)
{
char *missedFirstMeeting = SearchList((char *)registrants[0], (char **)attendees, attendeeCount);
}
我的 missFirstMeeting 似乎可以正确调用单个值,但是当我尝试在循环中重复调用我的 SearchList 函数时,如下所示:
for (int i = 0; i < attendeeCount; i++) {
*missedFirstMeeting = SearchList((char *)registrants[i], (char **)attendees, attendeeCount);
}
我收到分段错误错误。对我来说,我似乎在做同样的事情,但只是重复调用 SearchList(),但显然有些问题是我没有看到的,因为我收到了分段错误错误。有什么想法吗?谢谢。
I've been trying to solve this bsearch homework problem for awhile now. I try using my code to first search for one entry like so:
int Compare(const void *a, const void *b);
void SortStudents(char *studentList[], size_t studentCount)
{
qsort(studentList, studentCount, sizeof(studentList[0]), Compare);
}
int Compare(const void *a, const void *b)
{
return (strcmp(*(char **)a, *(char **)b));
}
char *SearchList(char *key, char *list[], size_t num)
{
char **value = bsearch(&key, list, num, sizeof(list[0]), Compare);
return (value == 0 ? 0 : *value);
}
/*Determines which registrants did not attend the first meeting by searching for registrants
that are not in attendees set. */
void DisplayClassStatus(
const char *registrants[], size_t registrantCount,
const char *attendees[], size_t attendeeCount)
{
char *missedFirstMeeting = SearchList((char *)registrants[0], (char **)attendees, attendeeCount);
}
My missedFirstMeeting seems to work in calling out a single value properly, but when I try to repeatedly call my SearchList function in a loop like so:
for (int i = 0; i < attendeeCount; i++) {
*missedFirstMeeting = SearchList((char *)registrants[i], (char **)attendees, attendeeCount);
}
I get a segmentation fault error. To me it seems like I am doing the same thing, but just repeatedly calling the SearchList(), but obviously something is wrong that I do not see since I get that segmentation fault error. Any ideas? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
删除firstMeeting 的前导“*”:
Remove the leading '*' of firstMeeting:
好的,问题如下,您迭代
registrants
但当处理attendeeCount
项目时,您的 for 会停止。另外,如果missedFirstMeeting
是一个 char*,按照 tur1ng 所说,您需要删除前导 *。所以只需这样做:编辑:
如果您想保留所有
SearchList
返回值,那么您应该这样做:当然,在使用完
missedFirstMeeting
后,您应该释放分配的内存。Ok, so the problem is the following, you iterate over
registrants
but your for stops when it's proccessedattendeeCount
items. And also, ifmissedFirstMeeting
is a char*, do as tur1ng said, you need to remove the leading *. So just do this:Edit:
If you want to keep all of
SearchList
return values then you should do something like this:Of course, after you finish using
missedFirstMeeting
you should free the memory allocated.好的,您需要将函数的返回值放入变量中。我以编写 C++ 为生,我的老板永远不会接受这样的代码。这段代码非常难以阅读,更难调试。原因是因为您可以为变量设置监视。当您逐行浏览程序时,您还可以看到程序是如何链接的。调试器将列出您的名称空间中的所有变量及其相应的值。我敢打赌,当你重写这个代码以使其更具可读性时,你会自己找出问题所在。
Ok, you need to put the return values functions into a variable. I write C++ for a living my boss would never accept code like this. This code is very difficult to read and even harder to debug. The reason why is because you can setup watches for a variable. You can also see how the program is chaining as you step though the program line by line. A debugger will list all variables in your name space and their corresponding values. I bet money that while you are rewriting this to be more readable, you'll figure out the problem your self.
我发现您的代码(至少)有两个问题。
首先,琐碎但严重的是
for
循环应该比较 i 与registrantCount
而不是attendeeCount
。第二个Compare()应该这样写:您只需将void指针转换为char指针即可。修复这些应该可以修复您的 SegFault 错误。
补充: for 循环中对
missedFirstMeeting
的取消引用是两个主要问题之一。指针转换很难阅读。There are (at least) two problems I see with your code.
First, trivial but serious is that the
for
loop should compare i versusregistrantCount
notattendeeCount
.Second Compare() should be written:You simply need to cast the void pointers to char pointers. Fixing these should fix your SegFault errors.
Added: The dereferencing of
missedFirstMeeting
in the for loop is one of the two major problems.The pointer casting is just hard to read.问题是,尽管计算机是大块可变内存,但这并不是在对其进行编程时想象它们的最佳方式。你用这种方式对计算机进行了建模,但效果并不理想。
The problem is that although computers are big blocks of mutable memory, that's not the best way to imagine them when programming them. You modeled the computer that way, and it didn't quite work out.