RICHI150
RICHI150

Reputation: 1

SIGSEGV(segmentation fault) error in dev c++ stack algorithm (Lenguage c)

Good day, i need yours help to resolve this problem.i made a copystack function and its pop up when i input data from console i have the next message error SIGSEGV(segmentation fault) error, but dont pop up when i input data from my code, i leave the code of input data of a stack and the copystack function.

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


}

Upvotes: 0

Views: 127

Answers (1)

kocica
kocica

Reputation: 6467

Problem

As you mentioned, problem begins here

copiarPila(pila1,pila2);

In this function, you are declaring an pointer to struct, and passes him uninitialized.

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

And in function apilar you are accessing uninitialized memory and writing there

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

which causes undefined behavior and program probably crash.


Solution

Simply allocate memory for struct tpila *pila_aux and you won't get SIGSEGV after accessing/modifying its content. Dont forget to free this pointer.

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

You should also know

  • Why dont cast mallocs return value
  • It's good practice to write program source code in English, even if it's only for you AND especially if you are going to post in somewhere.

Upvotes: 1

Related Questions