Thread: How to solve this type of error?

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

    How to solve this type of error?

    I wrote a program and have the following type of error. I don't know where is the problem and how to fix it. Thank you.

    Program received signal SIGSEGV, Segmentation fault.
    0x08048e1c in print_polynomial (n=0x11eb60) at test11.c:216
    216 print_monomial(p->m);
    (gdb) print p
    $1 = (polynomial_node *) 0x53f63156
    (gdb) print p->m
    Cannot access memory at address 0x53f63156

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Make your pointers point some place valid before you start messing with them.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    These seem to be the same problems you had in this post. Have you implemented the changes Salem suggested there? If so, post your REVISED code and we will see if we can help.

    What we aren't going to do is:

    1. Do your work for you
    2. Do a complete rewrite of the code.
    3. Continue to help if you do not follow the advice given <-----very important
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    14
    Quote Originally Posted by AndrewHunter View Post
    These seem to be the same problems you had in this post. Have you implemented the changes Salem suggested there? If so, post your REVISED code and we will see if we can help.

    What we aren't going to do is:

    1. Do your work for you
    2. Do a complete rewrite of the code.
    3. Continue to help if you do not follow the advice given <-----very important



    I have rewritten the code. Last time, the error is using NULL to point to some place. But this time it seems it is not this case. So I don't know how to fix it. The new codes are in the following. I think the problem is the function insert_monomial.

    Code:
     
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <malloc.h>
    #include <math.h>
    struct node /*a node denotes a P_{i,k}^{l}*/
    {
     int l,i,k;
    };
    typedef struct node node;
     
     
    struct monomial_node /* a monomial denotes a monomial P_{i,k}^{l} \cdots P_{i',k'}^{l'} */
    {
     node y;
     int coeff;         /* coeff is s(m_r) */
     struct monomial_node *next;  
    };
    typedef struct monomial_node monomial_node;
    typedef monomial_node *monomial;
    struct polynomial_node /* a polynomial denotes a sum of some monomials */
    {
     monomial m;
     struct polynomial_node *next; 
    };
    typedef struct polynomial_node polynomial_node;
    typedef polynomial_node *polynomial;
    monomial initial_monomial(void) /* construct an empty monomial with head, return the head pointer of the monomial */
    {
      monomial head=malloc(sizeof(monomial_node));
      head->next=NULL;
      return head;   
    }
    polynomial initial_polynomial(void) /* construct an empty polynomial with head, return the head pointer of the polynomial */
    {
      polynomial head=malloc(sizeof(polynomial_node));
      head->m=malloc(sizeof(monomial_node));
    
      head->next=NULL;
      return head;   
    }
     
    monomial insert_node(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 head;     
     monomial_node *s;
     
     monomial_node *p;
     
     head=b;
     p=head;
     
      s=malloc(sizeof(monomial_node));        /* create new node */
      s->y=a;
     if(p->next == NULL)
     {
       
        s->next=NULL;    
        head->next=s;                  /* insert the node a */
     }
     else if(p->next->next==NULL)
     {  
        p=p->next;
        if((p->y).i > a.i || (((p->y).i == a.i) && ((p->y).l > a.l)) || (((p->y).i == a.i) && ((p->y).l == a.l) && ((p->y).k >= a.k)))
        {
       	 s->next=p;    
       	 head->next=s;                  /* insert the node a */
        }
        else
        {
              s->next=NULL;    
      	  p->next=s;                  /* insert the node a */
        }
     }
     else
     {
       
       if((p->next->y).i > a.i || (((p->next->y).i == a.i) && ((p->next->y).l > a.l)) || (((p->next->y).i == a.i) && ((p->next->y).l == a.l) && ((p->next->y).k >= a.k)))
       {
          s->next=p->next;
          head->next=s;
       }
       else
       {
      	 while((p->next->y).i <= a.i && (((p->next->y).i != a.i) || ((p->next->y).l <= a.l)) && (((p->next->y).i != a.i) || ((p->next->y).l != a.l) || ((p->next->y).k < a.k)))          /* find the position */
      	 {
       	   p=p->next;
      	   if(p->next==NULL)
       	   {
       	    break;
       	   }
     	 }
             
      	  s->next=p->next;     
      	  p->next=s;           /* insert the node a */
       }
     }
     return head;
    }
    Last edited by lijr07; 06-26-2011 at 10:14 AM.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I am guessing you are NOT a good enough detail programmer to set next to a valid location.

    I suggest YOU no longer do malloc for any structure having a next except for a simple function that wraps the malloc with the setting of next to NULL.

    It may be you tried this with initial_monomial; but, you DO NOT use it every where. And, the comment implies more that I would have it do.

    Example place where it would be good to use the function is here.
    Code:
    s=malloc(sizeof(monomial_node));        /* create new node */
    Tim S.
    Last edited by stahta01; 06-26-2011 at 08:35 AM.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    14
    How to use gdb to set next to a valid location?

    Quote Originally Posted by stahta01 View Post
    I am guessing you are NOT a good enough detail programmer to set next to a valid location.

    I suggest YOU no longer do malloc for any structure having a next except for a simple function that wraps the malloc with the setting of next to NULL.

    It may be you tried this with initial_monomial; but, you DO NOT use it every where. And, the comment implies more that I would have it do.

    Example place where it would be good to use the function is here.
    Code:
    s=malloc(sizeof(monomial_node));        /* create new node */
    Tim S.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by lijr07 View Post
    How to use gdb to set next to a valid location?
    I have no idea; I write my source code so it does not have this type of problem.

    Tim S.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by lijr07 View Post
    How to use gdb to set next to a valid location?
    There's no way to know, just off the top of your head, where a "valid location" is going to be. That's the point of malloc, is to set aside a chunk of memory for you and tell you where it is. Until you do that, you don't even have any valid locations at all. Trying to solve this in gdb is about two steps too late. Perhaps instead of letting gdb run until it crashes, you should go step by step and find out where this p snuck through your defenses?

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    An example of what I suggested you do; NOTE: This code is untested.

    Tim S.

    Code:
    monomial alloc_monomial(void) 
    /* 
        Alloc an empty monomial with all parts 
        set to a value; return a pointer to it.  
    */
    {
      monomial ptr=malloc(sizeof(monomial_node));
      ptr->next=NULL;
      ptr->coeff=0;
      ptr->y.l=0;
      ptr->y.i=0;
      ptr->y.k=0;
      return ptr;   
    }

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Wouldn't the use of calloc work here as well?

    Code:
    monomial_node *monomial_ptr = calloc(1, sizeof(*monomial_ptr));
    calloc(3): Allocate/free dynamic memory - Linux man page

    calloc() allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated memory. The memory is set to zero.
    Emphasis mine.

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Look, clearly you are having some basic concept problems here. What I would do is to start a brand new empty program and go through and create a very simplistic list and practice these ideas until you get them down. Without understanding the fundamentals all you will do is to continue to run into these problems again and again.

    In summary:

    1. Start simple - walk before run
    2. Actually read and do some work to understand the base concept
    3. Follow the advice given, if you do not understand just say so. Just throwing code at the problem will never fix the problem and will just wind up in everyone here ignoring you.
    4. LOOK BELOW AND CLICK ON THE RED THINGS WITH UNDERLINES

    A tutorial on linked lists

    Linked list issues.

    Another tutorial.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #12
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Quote Originally Posted by rags_to_riches View Post
    Wouldn't the use of calloc work here as well?

    Code:
    monomial_node *monomial_ptr = calloc(1, sizeof(*monomial_ptr));
    calloc(3): Allocate/free dynamic memory - Linux man page



    Emphasis mine.
    Almost certainly it would, but to be completely portable you can't assume that setting a pointer to all bits zero does anything useful. It probably will on any platform you'll run into, but there's no guarantee.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-23-2011, 02:04 PM
  2. how to solve this error msg
    By rhythm in forum C++ Programming
    Replies: 11
    Last Post: 02-08-2008, 10:05 PM
  3. error cant solve
    By developer47 in forum C Programming
    Replies: 1
    Last Post: 04-30-2007, 05:10 AM
  4. please help me to solve the error
    By Jasonymk in forum C++ Programming
    Replies: 8
    Last Post: 05-02-2003, 09:08 AM
  5. ^^ help me solve the error
    By skwei81 in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2003, 09:04 AM