C 结构的快速排序会导致分段错误
该程序从文件中逐行读取并将信息存储在结构中。除了对结构数组进行排序之外,一切正常。例如,最后当我打印结构(最后包含的代码)时,它工作得很好。
当我调用 qsort 时,出现问题(分段错误)。
另外,打印 Students[0].lastName 工作正常,但打印 Students[1].lastName 返回 (null),这也令人困惑。
我到处都看过,我的代码似乎与作为排序结构的正确解决方案发布的代码非常相似,所以我很困惑。
在main的头中定义struct:
// DEFINE STRUCT
typedef struct _StudentInformation {
int term;
int studentId;
char *lastName;
char *firstName;
char *subject;
int catalogNumber;
char *section;
} StudentInformation;
在main方法中分配struct(STUDENT_DATA=50):
// ALLOCATE AN ARRAY OF STUDENTS (STRUCT)
StudentInformation *students;
if ((students = malloc(STUDENT_DATA*sizeof(StudentInformation)))==NULL) {
scanf("Error can't allocate enough students!\n");
exit(1);
}
问题:调用快速排序(8的原因是因为有是 8 个有效且已加载的条目,即使少于 8 个也不起作用)。:
qsort(students, 8, sizeof(StudentInformation), comparator);
快速排序比较器:
int comparator (const void * a, const void * b) {
StudentInformation *s1 = (StudentInformation*)a;
StudentInformation *s2 = (StudentInformation*)b;
return strcmp(s1->lastName, s2->lastName);
}
我知道数据加载良好的原因是因为打印工作完全正常:
void printInformation (StudentInformation *students) {
// PRINT EVERYTHING
while(students->firstName!=NULL) {
printf("%-s, %s %15d %4d %4s%d %7s\n",
students->lastName,students->firstName,students->term,
students->studentId, students->subject,students->catalogNumber,
students->section);
// Increment
students=students+sizeof(StudentInformation);
}
}
它打印的内容(我只包含了打印的 8 个中的 2 个,没有打印 NULL):
Castille, Michael Jr 1201 103993269 CSE230 R03
Boatswain, Michael R. 1201 105515018 CSE230 R01
谢谢!
The program is reading line by line from a file and storing info in a struct. Everything works except for sorting the array of structs. For example, at the end when I'm printing the struct(code included at the end), it works completely fine.
The problem (segmentation fault) occurs when I call the qsort.
Also, printing students[0].lastName works fine, but printing students[1].lastName returns a (null), that too is confusing.
I've looked everywhere and my code seems very similar to what has been posted as correct solutions to sorting structs, so I'm very confused.
Defining struct in header of main:
// DEFINE STRUCT
typedef struct _StudentInformation {
int term;
int studentId;
char *lastName;
char *firstName;
char *subject;
int catalogNumber;
char *section;
} StudentInformation;
Allocating the struct in main method (STUDENT_DATA=50):
// ALLOCATE AN ARRAY OF STUDENTS (STRUCT)
StudentInformation *students;
if ((students = malloc(STUDENT_DATA*sizeof(StudentInformation)))==NULL) {
scanf("Error can't allocate enough students!\n");
exit(1);
}
The problem: Calling quicksort (the reason for the 8 is because there are 8 entries THAT WORK and are LOADED, even less than 8 doesn't work).:
qsort(students, 8, sizeof(StudentInformation), comparator);
Comparator for quicksort:
int comparator (const void * a, const void * b) {
StudentInformation *s1 = (StudentInformation*)a;
StudentInformation *s2 = (StudentInformation*)b;
return strcmp(s1->lastName, s2->lastName);
}
The reason I know data is loaded fine is because printing works completely fine:
void printInformation (StudentInformation *students) {
// PRINT EVERYTHING
while(students->firstName!=NULL) {
printf("%-s, %s %15d %4d %4s%d %7s\n",
students->lastName,students->firstName,students->term,
students->studentId, students->subject,students->catalogNumber,
students->section);
// Increment
students=students+sizeof(StudentInformation);
}
}
What it prints (i only included 2 out of the 8 that were printed, no NULLS printed):
Castille, Michael Jr 1201 103993269 CSE230 R03
Boatswain, Michael R. 1201 105515018 CSE230 R01
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该行:
为结构本身分配内存,但不为指针引用的字符串分配内存:
每个字符串都为指针占用足够的内存。您需要分别为字符串分配内存:
写入不属于您的内存始终是调试弹跳错误时获得意外教训的好方法:您最终可能会遇到段错误或内存损坏,但是它可能位于似乎与问题的实际来源完全无关的代码区域中。
The line:
allocates memory for the structure itself, but not for the strings that are referenced by pointers:
Each of those occupies enough memory for a pointer. You'll need to allocate memory for the strings separately:
Writing to memory that you don't own is always a good way to get an unexpected lesson in debugging ricochet-errors: you'll probably get a segfault or memory corruption eventually, but it may be in an area of code that appears to be totally unrelated to the actual source of the problem.
如果 STUDENT_DATA >= 8,则只有一种可能的解释,即您的一个或多个
lastName
字段从未初始化并包含 NULL 或垃圾。如果初始化这些字段的循环包含与打印出来的循环相同的错误(使用students=students+sizeof(StudentInformation)
而不是students++
),这就是原因。Provided STUDENT_DATA >= 8, there's only one possible explanation, which is that one or more of your
lastName
fields was never initialized and contains NULL or garbage. If your loop initializing these fields contained the same error as your loop printing it out (usingstudents=students+sizeof(StudentInformation)
instead ofstudents++
), that would be why.您说
调用快速排序(8的原因是因为有8个条目
。这是不正确的。您应该传递数组中的元素数量(在您的情况下是
STUDENT_DATA
)。You said
Calling quicksort (the reason for the 8 is because there are 8 entries
.This is incorrect. you should pass the number of elements in the array (
STUDENT_DATA
in your case).