gpt4 book ai didi

c - 我的程序在尝试从链接列表中删除元素时抛出异常

转载 作者:行者123 更新时间:2023-11-30 18:45:00 24 4
gpt4 key购买 nike

我有一个链接列表和两个函数 - 一个用于添加成员,另一个用于从列表中删除成员。当我尝试删除成员时,我在删除函数中遇到异常。

异常在行

if (strcmp((temp_person_ptr->name), name_to_remove))

异常显示 -> single_linked_list.exe 中 0x50C4EF18 (ucrtbased.dll) 处未处理的异常:0xC0000005:读取位置 0xDDDDDDDD 时发生访问冲突。发生了

完整程序如下 -


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include "globals.h"

extern struct person* starting_address;

struct person* delete_person(size_t* list_size_ptr, struct person* ptr_current_person)
{
printf("There are %u items in list \n", *list_size_ptr);

struct person* temp_person_ptr = starting_address, *to_save_person_ptr;
printf("The items in list are \n");
while (temp_person_ptr)
{
printf("%s \n", temp_person_ptr->name);
temp_person_ptr = temp_person_ptr->next_person_ptr;
}

char name_to_remove[MAX_NAME_LENGTH];
printf("Please enter the name to remove \n");
gets_s(name_to_remove, MAX_NAME_LENGTH - 1);

temp_person_ptr = starting_address;
to_save_person_ptr = starting_address; // Not required by logic - just to initialize for compiler
while (temp_person_ptr)
{
if (strcmp((temp_person_ptr->name), name_to_remove))
{
to_save_person_ptr = temp_person_ptr;
temp_person_ptr = temp_person_ptr->next_person_ptr;
}
else
{
// Since we are going to remove temp_person_ptr - we save it's next person_ptr in preceding person which is to_save_person_ptr
// Only if the person_ptr to be removed is NOT the first person
// For now - assume - one element name will match
if (temp_person_ptr != starting_address)
to_save_person_ptr->next_person_ptr = temp_person_ptr->next_person_ptr; // takes care if temp_person_ptr is the last one as well
else // Else the next person's address is the new starting address
starting_address = temp_person_ptr->next_person_ptr;

free(temp_person_ptr);
(*list_size_ptr)--;
}
}
return (ptr_current_person);
}

将元素添加到列表的部分如下所示(整个函数) -

struct person* add_person(size_t* list_size_ptr, struct person* ptr_current_person)
{

struct person *ptr_new_person;

ptr_new_person = (struct person*) malloc(sizeof(struct person));

// If first person- its starting address is the starting address of list
if ((*list_size_ptr) == 0)
{
starting_address = ptr_new_person;
}
else
{
//1. Add the new address to the chain - only if this is not the first person
ptr_current_person->next_person_ptr = ptr_new_person;
}
ptr_new_person->next_person_ptr = NULL;

printf("Please enter the name \n");
gets_s(ptr_new_person->name, MAX_NAME_LENGTH - 1);

// 2. We may make ptr_new_person as ptr_current_person
ptr_current_person = ptr_new_person;
// 3. Now onwards ptr_current_person refers to the pointer to the newly added person

(*list_size_ptr)++;
return (ptr_current_person);
}

最佳答案

while (temp_person_ptr)
{
if (strcmp((temp_person_ptr->name), name_to_remove))
{
to_save_person_ptr = temp_person_ptr;
temp_person_ptr = temp_person_ptr->next_person_ptr;
}
else{...}

从这段代码来看,temp_person_ptr 似乎指向某个东西,但 ->name 为 NULL。在 strcmp 之前添加如下 printf 语句:

if(!(temp_person_ptr->name)){
printf("This is why your segfaulting\n");
}

您将看到 ->name 为 null 或其他内容。祝你好运

关于c - 我的程序在尝试从链接列表中删除元素时抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55944864/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com