c - 搜索不存在的项目时,链表出现分段错误
问题描述
我正在开发一个客户端/服务器程序,客户端输入姓名、姓氏和电话号码。输入功能运行良好。它通常会添加到链表中。当我使用服务器上的链接列表中已经存在的电话号码调用搜索功能时,它可以正常工作,但是当我尝试搜索电话号码时,服务器崩溃,说明分段错误,而它应该只是发送给客户端搜索不成功/未找到。仅当链表不为空时才会出现此错误。如果我在空列表中搜索不存在的数字,它会像在 if 语句中一样响应。
我尝试了很多选择,但我只是不知道哪里出了问题。
服务器
else if(strcmp(data_char_array,"output") == 0)
{
printf("[REQ] Client has requested a search\n");
return_value = read(client_info, izpis.phone_number, 100);
entry.phone_number[return_value] = 0;
if(ifExists(izpis.phone_number) == 1)
{
write(client_info, "none", 8);
printf("\n No match found!\n");
}
else
{
struct phonebook* find = findNum(izpis.phone_number);
char phone_num[101];
char last_n[31];
char first_n[31];
strncpy(phone_num, find->phone_number, 100);
strncpy(last_n, find->last_name, 30);
strncpy(first_n, find->first_name, 30);
write(client_info, "found",10);
write(client_info, first_n,100);
write(client_info, last_n,30);
write(client_info, phone_num, 30);
printf("\n Request processed succesfully\n");
}
struct phonebook* findNum(char phone[101]){
struct phonebook* curr = head;
if(head == NULL){
return NULL;
}
while(strcmp(curr->phone_number,phone))
{
if(curr->phone_number == NULL)
{
return NULL;
}
else
{
curr = curr->next;
}
}
return curr;
}
int ifExists(char phone[101]){
struct phonebook* curr = head;
if(head == NULL){
return 1;
}
while(strcmp(curr->phone_number,phone))
{
if(curr->phone_number == NULL)
{
return 1;
}
else
{
curr = curr->next;
}
}
return 0;
}
客户
char send[11] = "output";
char recieve[11];
char name[31];
char last[31];
char phone[101];
while(1)
{
return_value = write(data_socket, send, strlen(send));
if (return_value == -1)
{
perror("Error occurred while sending! Error at 0x1!");
exit(-1);
}
return_value = write(data_socket, argv[1], strlen(argv[1]));
if (return_value == -1)
{
perror("Error occurred while sending! Error at 0x2!");
exit(-1);
}
return_value = read(data_socket, recieve, 11);
recieve[return_value] = 0;
printf("Searching: %s\n", recieve);
if(strcmp(recieve, "found") == 0)
{
printf("Search was successful\n");
return_value = read(data_socket,name,31);
name[return_value] = 0;
printf("%s ", name);
return_value = read(data_socket,last,31);
last[return_value] = 0;
printf("%s ", last);
return_value = read(data_socket,phone,101);
phone[return_value] = 0;
printf("%s ", phone);
break;
解决方案
findNum
并且ifExists
实际上是重复的,相同的过程,但只是返回不同的结构。建议只合并findNum
。
struct phonebook* findNum(char phone[101]){
struct phonebook* curr = head;
if(head == NULL){
return NULL;
}
while(curr != NULL) {
if(curr->phone_number != NULL) {
if (strcmp(curr->phone_number, phone) == 0) {
return curr;
} //end if
} //end if
curr = curr->next;
}
return NULL;
}
然后,还需要更改函数调用
struct phonebook* find = findNum(izpis.phone_number);
if(find == NULL) {
write(client_info, "none", 8);
printf("\n No match found!\n");
}
else {
//struct phonebook* find = findNum(izpis.phone_number);
char phone_num[101];
char last_n[31];
char first_n[31];
strncpy(phone_num, find->phone_number, 100);
strncpy(last_n, find->last_name, 30);
strncpy(first_n, find->first_name, 30);
write(client_info, "found",10);
write(client_info, first_n,100);
write(client_info, last_n,30);
write(client_info, phone_num, 30);
printf("\n Request processed succesfully\n");
}
推荐阅读
- sql - DB2 分组和排序
- excel - 绕过代码以在单元格为空的地方插入单元格值
- typescript - 为什么打字稿将空字符串添加到条件返回的联合中?
- c# - 创建指向从程序集中动态加载的类的路由时出现问题?
- java - 依赖注入和@DependsOn 的区别
- php - php7.4、php7.4-fpm、http2、Apache、nginx
- javascript - 使用 javascript 加载外部页面的一部分
- templates - 在从 MailChimp 发送的 Outlook 上强制图像渲染和下载?
- apache-kafka - 当我使用基于时间的保留策略时,如果将“log.cleanup.policy”设置为“删除”和“压缩”,数据日志会发生什么情况
- flutter - 颤振等待来自firestore bool的信号