创建第二个WaitingRoom时触发Segmentation Fault问题求助
问题根源与修复方案
核心问题
你的代码触发段错误的原因是**createWaitingRoom函数未初始化WaitingRoom结构体中的next_S指针**。malloc分配的内存空间会保留随机垃圾值,而非自动初始化为NULL:
- 创建第一个
WaitingRoom时,可能巧合地next_S的随机值为NULL,因此insertinRoom的第一个if条件能正常判断; - 创建第二个
WaitingRoom时,next_S是野指针,此时执行strcmp(WR->next_S->name, name)会访问非法内存,直接触发段错误。
修复步骤
在createWaitingRoom函数中,添加对next_S的初始化,将其设为NULL:
WaitingRoom* createWaitingRoom(int ID, char* parola){ WaitingRoom* WR = (WaitingRoom*)malloc(sizeof(WaitingRoom)); // 检查malloc是否成功(可选但推荐) if (WR == NULL) { perror("malloc failed for WaitingRoom"); exit(EXIT_FAILURE); } strcpy(WR->parola, parola); WR->ID = ID; WR->next = NULL; WR->next_S = NULL; // 新增:初始化学生链表头指针 return WR; }
额外优化建议
- 检查
malloc返回值:在CreateStudent和createWaitingRoom中都应添加内存分配失败的判断,避免后续操作空指针。 - 字符串拷贝安全:使用
strncpy替代strcpy,避免目标数组越界:strncpy(S->name, name, sizeof(S->name)-1); S->name[sizeof(S->name)-1] = '\0'; // 确保字符串结尾 - 修正打印笔误:
printWaitingRoom中的Waiting Romom是拼写错误,应改为Waiting Room。
修复后的完整代码
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct studenti { char name[100]; struct studenti* next; } studenti; typedef struct WaitingRoom { int ID; char parola[100]; struct WaitingRoom* next; struct studenti* next_S; } WaitingRoom; studenti* CreateStudent(char* name) { studenti* S = (studenti*)malloc(sizeof(studenti)); if (S == NULL) { perror("malloc failed for student"); exit(EXIT_FAILURE); } strncpy(S->name, name, sizeof(S->name)-1); S->name[sizeof(S->name)-1] = '\0'; S->next = NULL; return S; } WaitingRoom* createWaitingRoom(int ID, char* parola){ WaitingRoom* WR = (WaitingRoom*)malloc(sizeof(WaitingRoom)); if (WR == NULL) { perror("malloc failed for WaitingRoom"); exit(EXIT_FAILURE); } strncpy(WR->parola, parola, sizeof(WR->parola)-1); WR->parola[sizeof(WR->parola)-1] = '\0'; WR->ID = ID; WR->next = NULL; WR->next_S = NULL; // 初始化学生链表头指针 return WR; } void insertinRoom(WaitingRoom* WR, char* name){ studenti* S = CreateStudent(name); if (WR->next_S == NULL || strcmp(WR->next_S->name, name) > 0) { S->next = WR->next_S; WR->next_S = S; } else { studenti* current = WR->next_S; while(current->next != NULL && strcmp(current->next->name, name) > 0) { current = current->next; } S->next = current->next; current->next = S; } } void printWaitingRoom(WaitingRoom* WR) { studenti* current = WR->next_S; printf("Waiting Room este %d cu parola %s\n", WR->ID, WR->parola); while (current != NULL) { printf("%s\n", current->name); current = current->next; } } void printall(WaitingRoom* WR){ WaitingRoom* current = WR; while (current != NULL) { printWaitingRoom(current); current = current->next; } } int main() { WaitingRoom* WR1 = createWaitingRoom(6969, "Muiedinamo"); insertinRoom(WR1, "BOOm1"); insertinRoom(WR1, "AOOm2"); insertinRoom(WR1, "DOOm3"); insertinRoom(WR1, "COOm4"); WaitingRoom* WR2 = createWaitingRoom(1234, "AStae"); insertinRoom(WR2, "B1"); insertinRoom(WR2, "AO2"); insertinRoom(WR2, "D3"); insertinRoom(WR2, "C4"); WR1->next = WR2; printWaitingRoom(WR1); printall(WR1); return 0; }
内容的提问来源于stack exchange,提问作者George Jac
相关产品推荐
相关产品推荐

