2017-08-17 134 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); 

你也應該知道