2017-08-17 5 views
0

こんにちは、この問題を解決するにはあなたの助けが必要です.iは、コンソールからデータを入力するときにcopystack関数とそのポップアップを作成しました。エラーSIGSEGV(セグメンテーションフォールト)エラーが、コードからデータを入力するとポップアップしません。スタックの入力データとcopystack機能のコードを残します。dev C++スタックアルゴリズムのSIGSEGV(Lenguage c)

/* 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 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); 

また、知っておくべき

  • Why dont cast mallocs return value
  • それはそれはあなたのためだけだとしても、英語でのプログラムのソースコードを記述することをお勧めだとあなたがポストしようとしている場合は特にどこかで
関連する問題