帮助解决函数中的分段错误

发布于 2024-08-22 13:06:39 字数 1277 浏览 4 评论 0原文

我已经尝试解决这个搜索作业问题有一段时间了。我尝试使用我的代码首先搜索一个条目,如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

晨光如昨 2024-08-29 13:06:39

删除firstMeeting 的前导“*”:

missedFirstMeeting = SearchList((char *)registrants[i], (char **)attendees, attendeeCount);

Remove the leading '*' of firstMeeting:

missedFirstMeeting = SearchList((char *)registrants[i], (char **)attendees, attendeeCount);
享受孤独 2024-08-29 13:06:39

好的,问题如下,您迭代 registrants 但当处理 attendeeCount 项目时,您的 for 会停止。另外,如果 missedFirstMeeting 是一个 char*,按照 tur1ng 所说,您需要删除前导 *。所以只需这样做:

for (int i = 0; i < registrantCount; i++) {
    missedFirstMeeting = SearchList((char *)registrants[i], (char **)attendees, attendeeCount);
    /* Code that uses missedFirstMeeting here */
}

编辑:
如果您想保留所有 SearchList 返回值,那么您应该这样做:

char** missedFirstMeething = malloc(sizeof(char*)*registrantCount);
for (int i = 0; i < registrantCount; i++) {
    missedFirstMeeting[i] = SearchList((char *)registrants[i], (char **)attendees, attendeeCount);
}

当然,在使用完 missedFirstMeeting 后,您应该释放分配的内存。

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.

酷到爆炸 2024-08-29 13:06:39

好的,您需要将函数的返回值放入变量中。我以编写 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.

只为一人 2024-08-29 13:06:39

我发现您的代码(至少)有两个问题。

首先,琐碎但严重的是 for 循环应该比较 i 与 registrantCount 而不是 attendeeCount

第二个Compare()应该这样写:

int Compare(const void *a, const void *b) 
{
    return (strcmp((char *)a, *(char *)b));
}

您只需将void指针转换为char指针即可。修复这些应该可以修复您的 SegFault 错误。

补充: for 循环中对 missedFirstMeeting 的取消引用是两个主要问题之一。

  for (i = 0; i < registrantCount; i++) {
      missedFirstMeeting = SearchList((char *)registrants[i], (char **)attendees, attendeeCount);
  }

指针转换很难阅读。

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.

大海や 2024-08-29 13:06:39

问题是,尽管计算机是大块可变内存,但这并不是在对其进行编程时想象它们的最佳方式。你用这种方式对计算机进行了建模,但效果并不理想。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文