1

This code is supossed to do some operations over polynomials (add, sub, mul,div) and everything is working beside division.

Looks like when I'm calling the add function inside of div function it crash for some reason, but I dont get why.

Here's the whole code, Im using a list to store the polynomials

The function called "dividir_polinomios" crash in this line:

p_aaux = sumar_polinomios(p_aaux,p_sumaaux);

I dont get why it crash, if I call the same function in Main, with the same polynomials, it works, I'm really having a bad time here because Im not used to program in C.

Here is the struct i use:

struct poli{

    float cons;
    unsigned char exp;
    struct poli *sig;

};

Here is the add function:

struct poli *sumar_polinomios(struct poli* p_a, struct poli *p_b){

    struct poli *res=NULL;
    struct poli *aux=NULL;
    struct poli *aux_a,*aux_b;
    aux_a=p_a;
    aux_b=p_b;
    while(aux_a || aux_b){
        aux=(struct poli*)malloc(sizeof(struct poli));
        if(aux_a->exp == aux_b->exp){
            aux->cons=aux_a->cons+aux_b->cons;
            aux->exp=aux_a->exp;
            aux->sig=NULL;
            res=crear_lista(res,aux);
            aux_a=aux_a->sig;
            aux_b=aux_b->sig;
        }
        else if(aux_a->exp > aux_b->exp){
            aux->cons=aux_a->cons;
            aux->exp=aux_a->exp;
            aux->sig=NULL;
            res=crear_lista(res,aux);
            aux_a=aux_a->sig;
        }
        else if(aux_a->exp < aux_b->exp){
            aux->cons=aux_b->cons;
            aux->exp=aux_b->exp;
            aux->sig=NULL;
            res=crear_lista(res,aux);
            aux_b=aux_b->sig;
        }
    }

    return res;

};

Here is the div function:

void dividir_polinomios(struct poli *p_a, struct poli *p_b){

    struct poli *p_aaux=p_a;
    struct poli *p_baux=p_b;
    struct poli *aux;
    struct poli *cuociente=NULL;
    struct poli *p_sumaaux=NULL;
    struct poli *p_ressuma;
    while(1){

        //CONDICION PARA ROMPER WHILE AQUI//

        //Termino del cuociente
        aux = (struct poli*)malloc(sizeof(struct poli));
        aux->cons = p_aaux->cons/p_baux->cons;
        aux->exp = p_aaux->exp - p_baux->exp;
        aux->sig=NULL;

        cuociente=crear_lista(cuociente,aux);

        //printf("TEST: constante: %.2f\nexponente: %d",aux->cons,aux->exp);

        //Multiplicar termino por p_baux
        p_sumaaux=multiplicar_polinomios(p_baux,aux);

        //Invertir signo para realizar la suma
        p_sumaaux=cambiar_signo(p_sumaaux);

        printf("polinomio a:\n");
        mostrar_lista(p_aaux);
        printf("\npolinomio p_summaux:\n");
        mostrar_lista(p_sumaaux);
        //Sumar p_aaux + p_summaux

        printf("\n\nAhora exploto");
        p_aaux = sumar_polinomios(p_aaux,p_sumaaux); //<--THIS CRASH


        //Se quita el primer elemento
        p_aaux=p_aaux->sig;

        mostrar_lista(p_aaux);

   }

};

And here's the function i use to create the lists

struct poli *crear_lista(struct poli *lista,struct poli *nodo){
    struct poli *aux;
    if(!lista){
        lista=nodo;
    }
    else{
        aux=lista;

        while(aux->sig!=NULL){
            aux=aux->sig;
        }
        aux->sig=nodo;
    }
    return lista;
}

Basically, if I used this input: (I actually dont store the "x", just the constants and the exps).

Poly_a = -20x^5 + 37x^3 -8x^2 - 15x

Poly_b = 4x^2-5

it should store in "cuociente" the following result:

-5x^3+3x-2

But instead of that, the code crash when "sumar_polinomnios" is called inside of "dividir_polinomios".

EDIT:

I managed to fix the problem, the problem was in "sumar_polinomios" and that was because aux_b list reach NULL value faster than aux_a list, so when aux_b list is null and I did some comparation like

aux_a->exp == aux_b->exp

it crashed because aux_b was null, so I did this.

struct poli *sumar_polinomios(struct poli** p_a, struct poli **p_b){

    struct poli *res=NULL;
    struct poli *aux=NULL;
    struct poli *aux_a,*aux_b;
    aux_a=(*p_a);
    aux_b=(*p_b);

    while(aux_a || aux_b){
        aux=(struct poli*)malloc(sizeof(struct poli));
        //printf("\nestoy aqui\n");
        //printf("\nexp_a:%d\texp_b:%d\n",aux_a->exp,aux_b->exp);
        if(aux_a && aux_b){
            if(aux_a->exp == aux_b->exp){
                //printf("exponentes iguales\n");
                aux->cons=aux_a->cons+aux_b->cons;
                aux->exp=aux_a->exp;
                aux->sig=NULL;

                crear_lista(&res,aux);
                aux_a=aux_a->sig;
                aux_b=aux_b->sig;
                //printf("fin exponentes iguales\n");

            }
            else if(aux_a->exp > aux_b->exp){
                //printf("exponenten a > exponente b");
                aux->cons=aux_a->cons;
                aux->exp=aux_a->exp;
                aux->sig=NULL;
                crear_lista(&res,aux);
                aux_a=aux_a->sig;
                //printf("fin exponenten a > exponente b");
            }
            else if(aux_a->exp < aux_b->exp){
                //printf("exponente a < exponente b");
                aux->cons=aux_b->cons;
                aux->exp=aux_b->exp;
                aux->sig=NULL;
                crear_lista(&res,aux);
                aux_b=aux_b->sig;
                //printf("fin exponente a < exponente b");
            }
        }
        else if(aux_a!=NULL){
            aux->cons=aux_a->cons;
            aux->exp=aux_a->exp;
            aux->sig=NULL;
            crear_lista(&res,aux);
            aux_a=aux_a->sig;

        }
        else{
            aux->cons=aux_b->cons;
            aux->exp=aux_b->exp;
            aux->sig=NULL;
            crear_lista(&res,aux);
            aux_b=aux_b->sig;

        }
    }

    return res;

}

Now I check if both list aren't NULL, and if one of them is null i just append the not-null list to the result list.

10
  • 3
    My immediate reaction is "Is that really an MCVE (minimal reproducible example)?" My hope is that there is more code there than necessary to show the problem, because (for Stack Overflow) that's quite a lot of code. Also, you need to show the sample input, the current output and the expected output and an explanation of what's different. Why isn't the repeated code that reads polynomials factored out into a function? Commented May 29, 2016 at 1:10
  • 1
    Incidentally, functions are not terminated with }; — that has an empty declaration after the function. Some compilers complain; others don't. Commented May 29, 2016 at 1:14
  • 1
    Why isn't the division function returning a value? Does it leak the allocated polynomials (or would it, if it didn't crash)? Is that a good idea? Commented May 29, 2016 at 1:29
  • 1
    Please provide a minimal reproducible example. That is, strip down your code to the "minimal complete code" which reproduces your problem. I don't see the whole code nor the snippets shown now will be helpful. Commented May 29, 2016 at 1:43
  • 1
    @Olaf: GCC permits a semicolon on its own under fairly excruciating warning options (gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -Werror -c x.c), but if you add -pedantic, it 'fesses up: x.c:1:1: error: ISO C does not allow extra ‘;’ outside of a function [-Werror=pedantic] when the file x.c contains a semicolon and a newline. So no, under pedantic rules, a semicolon after the close brace is not allowed, but GCC is a kind, tolerant compiler and you have to tell it to reject such code before it does so. Commented May 29, 2016 at 1:49

1 Answer 1

2

You have a double free() bug.

What's happening in crear_lista is you allocate a new head and graft an old tail on from a previously extant list, and what's happening in monstr_lista is you free the whole list. The second time this happens, the heap gets corrupted, so malloc() eventually returns garbage and you crash.

Solution: copy the nodo list in crear_lista.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.