gpt4 book ai didi

c - 为什么在尝试填充结构时出现段错误(核心转储)或总线错误(核心转储)?

转载 作者:太空宇宙 更新时间:2023-11-04 07:53:18 25 4
gpt4 key购买 nike

所以我尝试使用指向 MonsterAttacks 结构的指针作为属于链表元素的数据。为了做到这一点,我尝试填充一个 MonsterAttacks 结构,然后将它与一个 null ptr 一起传递到下一个节点到一个名为 create 的函数。然而,在 populate 方法的某处发生了段错误。我正在处理三个文件 list_demo.clinked_list.hlinked_list.c。我将构建构成功能齐全的链表的所有函数,希望我能尽快通过此错误。处理这个错误大约两天了,我给我的教授看了,他不明白为什么会这样,它似乎来自 populate 函数。我试图返回一个指向 strut 的指针,在这种情况下我得到一个总线错误,并且我已经尝试了几乎所有获取输入并将其存储在 strut 上的变体。我什至删除了该函数并尝试将其填充到 main 中,但没有任何效果。我是 C 的新手,我的教授帮助我调试了大约一个小时,但他最终放弃了,所以任何帮助都将不胜感激。

list_demo.c

 #include <stdio.h>
#include "linked_list.h"
#include <stdlib.h>



void populate(struct MonsterAttacks *m){

printf("Enter the name for the Monster \n");
scanf("%40s",m->monsterName);
puts("What is his/her attack location?");
scanf("%40s",m->attackLocation);
puts("What are the number of victims this monster has demolished?");
scanf("%ud", &m->numOfVictims);
m->attackID = 0;
}

int main(void)
{

node* tmp = NULL;
struct MonsterAttacks *tmpMonst = (struct MonsterAttacks *)
malloc(sizeof(struct MonsterAttacks));

if(tmpMonst == NULL){
printf("Error allocating memory");
}
else
populate(tmpMonst);

node *head = create(tmpMonst,tmp);

free(tmpMonst);
return 0;
}

linked_list.h

#ifndef LINKED_LIST
#define LINKED_LIST


typedef struct node{
struct MonsterAttacks *monsterAttack;
struct node* next;
} node;


struct MonsterAttacks{
unsigned int attackID;
char monsterName[41];
char attackLocation[41];
unsigned int numOfVictims;
};

/*
create a new node
initialize the data and next field

return the newly created node
*/
node* create(struct MonsterAttacks *m,node* next);



#endif

linked_list.c

//来自zentut.com,大量改编

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "linked_list.h"


/*
create a new node
initialize the data and next field
return the newly created node
*/
node* create(struct MonsterAttacks *m,node* next)
{
node* new_node = (node*)malloc(sizeof(node));
if(new_node == NULL)
{
printf("Error creating a new node.\n");
exit(0);
}

new_node->monsterAttack->attackID = 0;

new_node->next = next;

strncpy(new_node->monsterAttack->monsterName,m->monsterName,41);
strncpy(new_node->monsterAttack->attackLocation, m->attackLocation, 41);
new_node->monsterAttack->numOfVictims = m->numOfVictims;

return new_node;
}

顺便说一句,使用 gcc 编译器在 Red Hat 上运行

最佳答案

new_node->monsterAttack->attackID = 0;

new_node 分配内存不会为其内部的 MonsterAttacks 结构分配内存。这就是为什么取消引用 monsterAttack 以获取其 attackID 会导致段错误。

一个最小的工作代码

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
// Moved the two structs out to make a minimal reproducible code
/* #include "linked_list.h" */

struct MonsterAttacks{
unsigned int attackID;
char monsterName[41];
char attackLocation[41];
unsigned int numOfVictims;
};

typedef struct node{
struct MonsterAttacks *monsterAttack;
struct node* next;
} node;

void populate(struct MonsterAttacks *m){

printf("Enter the name for the Monster \n");
scanf("%40s",m->monsterName);
puts("What is his/her attack location?");
scanf("%40s",m->attackLocation);
puts("What are the number of victims this monster has demolished?");
scanf("%ud", &m->numOfVictims);
m->attackID = 0;
}

node* create(struct MonsterAttacks *m,node* next)
{
node* new_node = (node*)malloc(sizeof(node));
if(new_node == NULL)
{
printf("Error creating a new node.\n");
exit(0);
}

// Just add this line
new_node->monsterAttack = malloc(sizeof (struct MonsterAttacks));

new_node->monsterAttack->attackID = 0;
new_node->next = next;

strncpy(new_node->monsterAttack->monsterName,m->monsterName,41);
strncpy(new_node->monsterAttack->attackLocation, m->attackLocation, 41);
new_node->monsterAttack->numOfVictims = m->numOfVictims;

return new_node;
}

int main(void)
{
node* tmp = NULL;
struct MonsterAttacks *tmpMonst = (struct MonsterAttacks *)
malloc(sizeof(struct MonsterAttacks));

if(tmpMonst == NULL){
printf("Error allocating memory");
}
else {
populate(tmpMonst);
}

node *head = create(tmpMonst,tmp);

printf("Name: %s\n", tmpMonst->monsterName);
printf("num victim: %d\n", tmpMonst->numOfVictims);

free(tmpMonst);
return 0;
}

当您在 create(...) 中为 new_node 分配内存时,您在堆上为 node 类型的结构分配内存保存它包含的所有变量。在这种情况下,node 中的 monsterAttack 最初是一个指向无处可去的结构的指针。您需要为要指向的 monsterAttack 指针显式分配内存。

关于c - 为什么在尝试填充结构时出现段错误(核心转储)或总线错误(核心转储)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52435728/

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