What's wrong with this program?

This is a discussion on What's wrong with this program? within the C Programming forums, part of the General Programming Boards category; The link of the program is: <<<snipped>>> I use gdb to debug the program. It said that: Program received signal ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    14

    What's wrong with this program?

    The link of the program is: <<<snipped>>> I use gdb to debug the program. It said that: Program received signal SIGSEGV, Segmentation fault.
    0x080491c7 in simplify_polynomial (n=0x804c148) at test7.c:476
    476 p=p->head->next;
    I don't know what is the problem. Could you help me? Thank you.
    Last edited by Salem; 06-24-2011 at 12:18 AM. Reason: Snip link to nowhere

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Post your code here... in code tags... post your error messages... ask specific questions.

    EDIT... there's no program code at that link... The Mods should close this thread.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    14

    The code is in the following. Thank you.

    [code]

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <malloc.h>
    #include <math.h>



    typedef struct node /*a node denotes a P_{i,k}^{l}*/
    {

    int l,i,k;

    } node;


    typedef struct monomial_node /* a monomial denotes a monomial P_{i,k}^{l} \cdots P_{i',k'}^{l'} */

    {
    node y;
    int coeff; /* coeff is the coefficent of the monomial */
    struct monomial_node *next;
    struct monomial_node *head;
    } monomial_node, *monomial;







    typedef struct polynomial_node /* a polynomial denotes a sum of some monomials */

    {

    monomial m;

    struct polynomial_node *next;

    struct polynomial_node *head;

    } polynomial_node, *polynomial;





    monomial insert(node a, monomial b) /* insert a factor a to a monomial b such that the first subscripts i's in the new monomial is in increasing order */

    {

    monomial p;

    monomial_node *s;

    monomial_node *p1;


    p=b;
    p1=p->head->next;

    if(p1==NULL)
    {

    s=malloc(sizeof(monomial_node));
    s->y=a;
    p->head->next=s;
    p->head->next->next=NULL;
    p->coeff=1;
    }
    else
    {
    while((p1->y).i >= a.i) /* find the position */
    {

    if(p1->next==NULL)
    {

    break;

    }
    p1=p1->next;

    }
    }


    s=malloc(sizeof(monomial_node)); /* create new node */

    s->y=a;

    s->next=p1->next; /* insert the node a */

    p1->next=s;

    p->coeff=b->coeff;
    return p;

    }


    monomial create_monomial(int number) /* create a monomial */

    {

    monomial m;

    monomial_node *p, *s;

    int i;


    m=(monomial)malloc(sizeof(monomial_node));
    m->head=(monomial)malloc(sizeof(monomial_node));

    if(m->head==NULL)

    {
    printf("No memory.\n");
    return NULL;

    }


    m->head->next=NULL;

    p=malloc(sizeof(monomial_node));

    m->head->next=p;

    p->next=NULL;


    printf("Please input the parameters of the heighest weight monomial(for example, 2, 2, 3 denotes the monomial Y^{2}_{2,3}). The first subscripts i of Y's should be in increase order.");

    scanf("%d", &((p->y).l));
    scanf("%d", &((p->y).i));
    scanf("%d", &((p->y).k));

    if(number > 1)
    {
    for(i=0;i<number-1;i++)

    {

    s=(monomial)malloc(sizeof(monomial_node));

    scanf("%d", &((s->y).l));
    scanf("%d", &((s->y).i));
    scanf("%d", &((s->y).k));




    m=insert(s->y,m);


    }

    }

    m->coeff=1;



    return m;



    }



    polynomial create_polynomial(monomial n) /* create a polynomial with only one monomial n */

    {

    polynomial m;
    polynomial_node *p;

    m=(polynomial)malloc(sizeof(polynomial_node));
    p=(polynomial)malloc(sizeof(polynomial_node));
    m->head=(polynomial)malloc(sizeof(polynomial_node) );

    m->head->next=p;
    p->m=n;
    p->next=NULL;



    return m;



    }





    void print_monomial(monomial m)

    {

    monomial p;


    p=m->head->next;

    if(p==NULL)

    {

    printf("No element. \n");

    }

    if(m->coeff == -1)
    {
    printf("-");
    }
    else if(m->coeff != 1)
    {
    printf("%d", m->coeff);
    }

    while(p!=NULL)

    {

    printf("Y^{%d}_{%d,%d}",(p->y).l,(p->y).i,(p->y).k);

    p=p->next;

    }

    }
    Last edited by lijr07; 06-24-2011 at 07:23 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    > 476 p=p->head->next;
    > I don't know what is the problem. Could you help me? Thank you.
    You should check p->head isn't NULL before trying to do p=p->head->next


    What are these for?
    Code:
    typedef struct monomial_node /* a monomial denotes a monomial P_{i,k}^{l} \cdots P_{i',k'}^{l'} */
    {
     node y;
     int coeff;         /* coeff is the coefficent of the monomial */
     struct monomial_node *next; 
     struct monomial_node *head;
    } monomial_node, *monomial;
    
    typedef struct polynomial_node /* a polynomial denotes a sum of some monomials */
    {
     monomial m;
     struct polynomial_node *next; 
     struct polynomial_node *head;
    } polynomial_node, *polynomial;
    If you've got say 5x^2+10x+20, then you have 3 polynomial nodes, where each instance of the 'm' member is initialised to 5x^2, 10x and 20

    I would also re-think the idea of storing a head pointer in each node. If you delete the head node of a list, you have to change the entire list to point to the new head node (of the list).

    Linked list - Wikipedia, the free encyclopedia
    Draw out some pictures so you're clear on paper exactly what is stored in a node, and how all the pointers are arranged.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    14
    These are a part of my program. Thank you.
    Last edited by lijr07; 06-24-2011 at 07:22 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what's wrong with this program?
    By fares jajeh in forum C Programming
    Replies: 5
    Last Post: 05-05-2011, 11:39 AM
  2. What is wrong with this program??
    By cprog12 in forum C Programming
    Replies: 3
    Last Post: 11-24-2010, 12:48 PM
  3. What is wrong with the following program?
    By roaan in forum C Programming
    Replies: 14
    Last Post: 08-27-2009, 01:52 PM
  4. What's wrong with my program?
    By Infuriate in forum C Programming
    Replies: 7
    Last Post: 12-03-2005, 04:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21