Help with segmentation fault in function
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);
}
/*D开发者_开发问答etermines 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.
Remove the leading '*' of firstMeeting:
missedFirstMeeting = SearchList((char *)registrants[i], (char **)attendees, attendeeCount);
Ok, so the problem is the following, you iterate over registrants
but your for stops when it's proccessed attendeeCount
items. And also, if missedFirstMeeting
is a char*, do as tur1ng said, you need to remove the leading *. So just do this:
for (int i = 0; i < registrantCount; i++) {
missedFirstMeeting = SearchList((char *)registrants[i], (char **)attendees, attendeeCount);
/* Code that uses missedFirstMeeting here */
}
Edit:
If you want to keep all of SearchList
return values then you should do something like this:
char** missedFirstMeething = malloc(sizeof(char*)*registrantCount);
for (int i = 0; i < registrantCount; i++) {
missedFirstMeeting[i] = SearchList((char *)registrants[i], (char **)attendees, attendeeCount);
}
Of course, after you finish using missedFirstMeeting
you should free the memory allocated.
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.
There are (at least) two problems I see with your code.
First, trivial but serious is that the for
loop should compare i versus registrantCount
not attendeeCount
.
Second Compare() should be written:
int Compare(const void *a, const void *b)
{
return (strcmp((char *)a, *(char *)b));
}
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.
for (i = 0; i < registrantCount; i++) {
missedFirstMeeting = SearchList((char *)registrants[i], (char **)attendees, attendeeCount);
}
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.
精彩评论