gpt4 book ai didi

c - 程序不运行(RUN FINISHED 分段故障核心转储)

转载 作者:行者123 更新时间:2023-11-30 19:44:57 24 4
gpt4 key购买 nike

我一直在尝试编写一个程序,编译器既不向我显示警告也不向我显示错误,只向我显示运行已完成的段错误核心转储,我想这与将值分配给指针,但我没有找到错误,你能帮我吗?

结构人

#ifndef PERSONSTRUCT_H
#define PERSONSTRUCT_H
#define TRUE 1
#define FALSE 0
#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
int edad;
char nombre[100];
}persona;

persona *getNodoPerson();
void setEdad(int, persona *);
void setNombre(char *, persona *);

#ifdef __cplusplus
}
#endif

#endif

结构节点

#define TRUE 1
#define FALSE 0
#ifndef LISTASIMPLEMENLAZADA_H
#define LISTASIMPLEMENLAZADA_H
#include "PersonStruct.h"

#ifdef __cplusplus
extern "C" {
#endif

struct nodo{
struct nodo *siguiente;
persona *person;
};

typedef struct nodo *ptrNodo;
typedef struct nodo *lista;

void freeNode(ptrNodo);
ptrNodo getNodo();
int empty(lista);
ptrNodo buscar(char[],int, lista);
int contar(lista);
void addAtEnd(char *, int, lista);
void del(lista,char *,int);
void imprimirDatos(lista);
int getPos(lista, persona *);
lista getLista();

#ifdef __cplusplus
}
#endif

#endif

结构体 nodo 的函数

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

//Esta funcion elimina de memoria el nodo enviado
void freeNode(ptrNodo p)
{
free(p);
}

//Esta funcion crea el espacio de memoria para un nodo
ptrNodo getNodo()
{
ptrNodo p;
p=(ptrNodo)malloc(sizeof(struct nodo));
return p;
}

lista getLista()
{
lista l;
l=(lista)malloc(sizeof(struct nodo));
l->person=NULL;
return l;
}

//Esta funcion revisa si una lista esta vacia;
int empty(lista l)
{
int x;
if(l->siguiente==NULL)
{
x=TRUE;
}
else
{
x=FALSE;
}

return x;
}

//Esta funcion cuenta el numero de elementos que tiene una lista enlazada
int contar(lista l)
{
int cont=0;
ptrNodo aux=getNodo();
aux=l->siguiente;
while(aux!=NULL)
{
aux=aux->siguiente;
cont++;
}
return cont;
}

//Esta funcion busca en la lista especificada, el nodo en el que se encuentra la estructura se encuentra especificada
//si la estructura es encontrada, devuelve un puntero al nodo donde esta la estructura
//si no devuelve un puntero nulo
ptrNodo buscar(char *nom,int age, lista l)
{
ptrNodo aux=getNodo(),retorno=getNodo();
retorno=NULL;
if(!empty(l))
{
aux=l->siguiente;
while(aux!=NULL)
{
if ((aux->person->edad==age) && (strcmp(aux->person->nombre,nom)==0))
{
retorno=aux;
break;
}
aux=aux->siguiente;
}
}
return retorno;
}

//Ingresa un nodo al final de la lista, si la lista es vacia, lo ingresa como el primer elemento de la lista
void addAtEnd(char *nombre,int age,lista l)
{
ptrNodo aux1=getNodo(),aux2=getNodo();
aux2->person->edad=age;
strcpy(aux2->person->nombre,nombre);
aux2->siguiente=NULL;
if(!empty(l))
{
aux1=buscar(nombre,age,l);
if(aux1==NULL)
{
aux1=l->siguiente;
while(aux1!=NULL)
{
aux1=aux1->siguiente;
}
aux1->siguiente=aux2;
}
else
{
printf("El valor ya se encuentra en la lista");
}
}
else
{
l->siguiente=aux2;
}

}

//Envia la posicion numerica del nodo en la lista, retornara 0 si no encuentra el nodo
int getPos(lista l, persona *p)
{
ptrNodo aux=getNodo();
int x=0;
if(!empty(l))
{
aux=buscar(p->nombre,p->edad,l);
if(aux!=NULL)
{
aux=l->siguiente;
while(aux->person->edad==p->edad && (strcmp(aux->person->nombre,p->nombre)==0))
{
x++;
aux=aux->siguiente;
}
}
}
return x;
}

//Este metodo elimina el valor de lista
void del(lista l,char *nom,int age)
{
ptrNodo aux1=getNodo(),aux2=getNodo();
int x,i;
if(!empty(l))
{
aux1=buscar(nom,age,l);
if(aux1!=NULL)
{
x=getPos(l,aux1->person);
aux2=l->siguiente;
for(i=1;i<=(x-1);i++)
{
aux2=aux2->siguiente;
}
aux2->siguiente=aux1->siguiente;
printf("\nLa persona %s y edad %d",aux1->person->nombre,aux1->person->edad);
freeNode(aux1);
}
}
}

void imprimirDatos(lista l)
{
ptrNodo aux=getNodo();
int i=1;
if(!empty(l))
{
aux=l->siguiente;
while(aux!=NULL)
{
printf("\nLa persona numero %d con \tNombre:%s y edad:%d",i,aux->person->nombre,aux->person->edad);
i++;
}
}
}

主要内容

#include <stdio.h>
#include <stdlib.h>
#include "ListaSimplemEnlazada.h"
#include "PersonStruct.h"

/*
*
*/
int main(int argc, char** argv) {
argc=argc;
argv=argv;

char *nombres[5]={"Alberto", "Manuel","Enrique","Josue","Ronald"};
int edad[5]={25,39,45,12,21};
int i;
lista l=getLista();
for (i=0;i<=4;i++)
{
addAtEnd(nombres[i],edad[i],l);
}
imprimirDatos(l);
del(l,nombres[0],edad[0]);
imprimirDatos(l
);

return (EXIT_SUCCESS);
}

抱歉,代码量很大,但也许您需要它来理解我的程序做错了什么

最佳答案

目前,您的函数 getNodo 只是 malloc 的前端。它返回一个新的但未初始化的节点,其成员可能包含垃圾。当您在 attAtEnd 中取消引用 aux2->person 时:

aux2->person->edad=age;

你会遇到段错误。您不仅必须分配,还必须填充节点,即用合理的数据填充它。 (该数据也可能意味着 aux2->person == NULL,但如果这是有效的,您应该在取消引用指针之前检查它。)

除了西类牙语和英语的奇怪混合之外,您的函数命名也不好:get 表明您正在尝试访问现有内存。您的函数可能应该命名为 newNodecrearNodo

关于c - 程序不运行(RUN FINISHED 分段故障核心转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27104182/

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