gpt4 book ai didi

c - 查看列表中下一个节点的地址时程序崩溃;只发生在非常奇怪的情况下

转载 作者:太空宇宙 更新时间:2023-11-04 08:52:33 27 4
gpt4 key购买 nike

第一次调用 addToEnd() 时它起作用,但第二次它使程序崩溃。我一直在尝试调试它,发现它发生在 head->next 发生时。我有点困惑,因为它只是被读取,这会使程序崩溃吗?如果是,你怎么可能遍历文件?

它似乎适用于 entry 的某些值,但不适用于其他值。如果两个条目相同并且全部由一个字母组成,它就会崩溃。因此,如果调用 addToEnd(head, "aaaaaaaaa") 然后调用 addToEnd(head, "aaaaaaaaa") 程序会崩溃,但如果调用 addToEnd(head, "aaaaaaaaa") 然后调用 addToEnd(head, "aaaaaaaab") 就没问题。

这是 100% 的代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

typedef struct node
{
char entry[21];
struct node* next;
} node;

void readDic();
void reverStr(char *str);
bool isInDic(char *reversed);
void addToEnd(node* head, char entry[21]);
unsigned int searchAndDestroy(node **head, char *entry);
void printList(node* head);
int main()
{
printf("Hello\n");
readDic();
printf("Goodbye!");
return EXIT_SUCCESS;
}

void readDic()
{
FILE* words;
char singleLine[21];
words = fopen("words.txt", "r");
node* head = malloc(sizeof(node));
fscanf(words, "%20s", head->entry);//need to do this initially
head->next = NULL;
printf("here 0");

printf("here 0.1");
if(words == NULL)
exit(EXIT_FAILURE);
printf("here 0.5");
while(fscanf(words, "%20s", singleLine) == 1)assigned input terms
{
printf("\nhere 0.6\n|%s|", singleLine);
addToEnd(head, singleLine);//problem here
printf("here 0.7");
reverStr(singleLine);
printf("here 1");
if(isInDic(singleLine)){
printf("here 2");
searchAndDestroy(&head, singleLine);
printf("here 3");
}
}
printf("here 4");
fclose(words);
printList(head);
printf("here 5");
}

//http://stackoverflow.com/questions/198199/how-do-you-reverse-a-string-in-place-in-c-or-c
/* PRE: str must be either NULL or a pointer to a
* (possibly empty) null-terminated string. */
void reverStr(char *str)
{
char temp, *end_ptr;

/* If str is NULL or empty, do nothing */
if(str == NULL || !(*str))
return;

end_ptr = str + strlen(str) - 1;

/* Swap the chars */
while( end_ptr > str )
{
temp = *str;
*str = *end_ptr;
*end_ptr = temp;
str++;
end_ptr--;
}
}

bool isInDic(char* reversed)
{
FILE* words;
char singleLine[21];

words = fopen("words", "r");
if(words == NULL)
exit(EXIT_FAILURE);
while(fscanf(words, "%20s", singleLine) == 1)//the length of the string has to be 1 less than declared size for the newline character
{
//printf("singline: %s reversed: %s\n", singleLine, reversed);
if(strcmp(singleLine, reversed) == 0)//strcmp returns 0 if both cstrings are equal
return true;
}
fclose(words);
return false;
}

void addToEnd(node* head, char entry[21])
{
printf("hi");
//printf("\naddress of next %p\n", (void *)head->next);
while(head->next != NULL)//just reading head->next screws it up
head = head->next;
printf("in addToEnd 2\n");
node* last = (node*)malloc(sizeof(node));
printf("in addToEnd 3\n");
head->next = last;
printf("in addToEnd 4\n");
strcpy(last->entry, entry);
printf("in addToEnd 5\n");
last->next = NULL;
printf("in addToEnd 6\n");
}

unsigned int searchAndDestroy(node **head, char *entry)
{
unsigned int count = 0;

while(*head)
{
node *del;
if(strcmp((*head)->entry, entry))
{ //this node stays
head = &(*head)->next;
continue;
}
/* this node goes
at this point head MUST point to the pointer that points at the node to be deleted
*/
del = *head;
*head = (*head)->next;
free(del);
count++;
}
return count; //number of nodes deleted
}

void printList(node* head)
{
printf("\nprinting everything\n");
if(head != NULL)
{
while(head->next != NULL)
{
printf("%s", head->entry);
head = head->next;
}
printf("%s", head->entry);
}
}

head 被设置为 null 的答案是正确的,但我看不到在哪里?

最佳答案

当您创建 head 时,您不会将 head->next 设置为 NULL

关于c - 查看列表中下一个节点的地址时程序崩溃;只发生在非常奇怪的情况下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19099753/

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