2017-08-17 39 views
0

好日子,我需要你们帮助解决这个问题。我做了一个copystack功能,当我从控制台输入数据时弹出它,我有下一个消息错误SIGSEGV(段错误)错误,但不要弹出时,当我从我的代码输入数据,我离开堆栈的输入数据的代码和copystack函数。在dev C++堆栈算法(冷语c)中出现SIGSEGV(段错误)错误

/* declaracion */ 
struct tpila{ 
    int clave; 
    struct tpila *sig; 
}; //Stack type 

void crear(struct tpila **pila) //Creation of stack function 
{ *pila = (struct tpila *) malloc(sizeof(struct tpila)); 
    (*pila)->sig = NULL; 
} 

int vacia(struct tpila *pila){ 
    return (pila->sig == NULL); 
} 

void apilar(struct tpila *pila, int elem){ //Stack input function 

    struct tpila *nuevo; 
    nuevo = (struct tpila *) malloc(sizeof(struct tpila)); 
    nuevo->clave = elem; 
    nuevo->sig = pila->sig; 
    pila->sig = nuevo; 
} 

void desapilar(struct tpila *pila, int *elem){ 
    struct tpila *aux; 

    aux = pila->sig; 
    *elem = aux->clave; 
    pila->sig = aux->sig; 
    free(aux); 
} 
void mostrar(struct tpila *pila)//Function print stack 
{ 

    struct tpila *aux; 

    aux=pila->sig; 

    while(aux!=NULL) 
    { 

      printf("%d->",aux->clave); 
      aux=aux->sig; 

    } 
    printf("NULL\n"); 

} 
void copiarPila(struct tpila *pila1,struct tpila *pila2)//Copystack function 
{ 

    struct tpila *pila_aux,*aux; 

    aux=pila1->sig; 

    //Llenamos la pila auxiliar 

    while(aux!=NULL) 
    { 
     apilar(pila_aux,aux->clave); 
     aux=aux->sig; 

    } 

    //Colocamos los datos de la pila auxiliar en la pila 2 

    aux=pila_aux->sig; 

    while(aux!=NULL) 
    { 
     apilar(pila2,aux->clave); 
     aux=aux->sig; 

    } 




} 


int main(void) 
{ 
    struct tpila *pila1,*pila2; 
    bool ingresar_datos=true; 
    int dato; 
    int desicion=2; 


    //Creation of stack 1 a stack 2 
    crear(&pila1); 
    crear(&pila2); 

    printf("Title\n"); 
    printf("-----------------------------------------------\n"); 


    //Colocamos valores a la pila1 


    while(ingresar_datos) 
    { 
    printf("Input a number\n"); 
    scanf("%d",&dato); 
    printf("\n"); 
    apilar(pila1,dato);//Input variable dato 
    printf("To stop input numbers press 2 \n"); 
    scanf("%d",&desicion); 
    system("cls"); 

    if(desicion==2) 
    { 
     ingresar_datos=false; 
    } 
    }   

    printf("Show stack 1 1\n"); 
    mostrar(pila1); 
    printf("-----------------------------------------------\n"); 
    printf("Show stack 2 2\n"); 
    mostrar(pila2); 
    printf("-----------------------------------------------\n"); 
    printf("Copy stack 1 to stack 2\n"); 
    copiarPila(pila1,pila2);----->In this part the program marks the problem 
    printf("-----------------------------------------------\n"); 
    printf("Show stack 2 \n"); 
    mostrar(pila2); 
    printf("-----------------------------------------------\n"); 
    system("pause"); 


} 

回答

1

问题

至于你提到的问题就从这里开始

copiarPila(pila1,pila2); 

在此功能中,您声明一个指向struct,并通过他的初始化。

struct tpila *pila_aux; 
apilar( pila_aux ,aux->clave); 

而且在功能apilar您正在访问未初始化的内存,写有

nuevo->sig = pila->sig; 
pila->sig = nuevo; 

这会导致不确定的行为和程序可能崩溃。


解决方案

简单的struct tpila *pila_aux分配内存和访问/修改其内容后,你不会得到SIGSEGV。不要忘记释放这个指针。

struct tpila *pila_aux = malloc(sizeof(struct tpila)); 
struct tpila *aux; 
// ... 
// Do stuff here ... 
// ... 
free(pila_aux); 

你也应该知道