Thread: ERROR: Program received signal SIGSEGV, Segmentation fault.

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    16

    ERROR: Program received signal SIGSEGV, Segmentation fault.

    Hi,
    I'm writting a C program for my university assignment in data structures. The program uses the radix(bucket) sort method to sort strings. One of the requirements is to use linked lists in sorting. I got no compile error,but when I runed the program I got an error that says: Program received signal SIGSEGV, Segmentation fault.

    It also refers to the location of the problem which is in the function isLast , here's the code :
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    #define MAXSIZ 15
    #define number_of_alpha 53
    
     typedef struct Node{
        char word[MAXSIZ];       // to hold the string
        struct node *next;         // to point to the next node
    }Node;
    
    
    typedef struct Node *PtrToNode;     // Pointer to a node (typedef)
    typedef PtrToNode Position;             // Pointer to a node
    typedef PtrToNode List;                    // Pointer that points to the header node
    
    void Sort();
    void add( char*,List);
    void moveNode(List,List,Position);
    void moveBack (Position,List);
    Position findPrevious(Position,List);
    int isLast(Position);
    void print(List);
    int empty(List);
    
    and the implementation of isLast :
    int isLast(Position p)        //parameter p points to a node
    {
        return p->next == NULL;    
    }
    I actually called this funtion in the add function in order to determine the last node.
    But I can't figure out the reason of this error , any help would be appreciated.
    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    How is the return value from isLast() being used by the add() function?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,658
    > return p->next == NULL;
    If p is NULL, then p->next is a segfault.
    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.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    16
    Here is the implementation of the add function and the isEmpty function:

    Code:
    void add( char* string, List header)
    {
        Position tmpNode;
        Position p;
        p = header;
        tmpNode = malloc(sizeof(struct Node));
        if(tmpNode == NULL)
            exit(0);
        strcpy(tmpNode->word,string);
    
    
          if(isEmpty(header))
           p->next = tmpNode;
    
           else
           {
               while(!isLast(p))
               p = p->next;
    
               p->next = tmpNode;
               tmpNode->next = NULL;
           }
    }
    
    int isEmpty(List header)
    {
        return (header->next == NULL);
    }

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    16
    so is there a way to check the last node ?

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Yep! it'd blow up for adding the header node unless you check if header == NULL instead of header->next == NULL or refer to Salem's last post.

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    16
    Do you mean to change function isEmpty to the following:
    Code:
    int empty(List header)
    {
        return (header == NULL);
    }
    If so, I tried it but it still gives me an error.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Christine View Post
    Do you mean to change function isEmpty to the following:
    Code:
    int empty(List header)
    {
        return (header == NULL);
    }
    If so, I tried it but it still gives me an error.
    Yep! it'll blow up because empty() returns 1 when header == NULL, and the next statement is p->next = tmpNode;.
    Nope! add the first node i.e. the header node in the add() function before proceeding to test if it isEmpty().
    Last edited by itCbitC; 02-07-2011 at 02:36 PM.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,676
    Code:
    typedef struct Node *PtrToNode;     // Pointer to a node (typedef)
    Ugh...hiding pointers, although a common practice, is a bad idea, particularly when you're learning.

    What error? Still a seg fault?

  10. #10
    Registered User
    Join Date
    Feb 2011
    Posts
    16
    Yes it's the seg fault.
    Well, it's that the header node is an element of an array. I declared an array of type struct node and each element of the array is considered a header node to a linked list. When I add nodes, I link an element of the array ( a header ) to that new node.

    So according to what itCbitC is saying, how can I add the header node before adding the next node?

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    16
    rags_to_riches why do you it's a bad idea ?

  12. #12
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Christine View Post
    ... how can I add the header node before adding the next node?
    Code:
    if (header is NULL)
        p = header = tmpNode;
    <rest of code>

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    2,676
    Because it hides the true nature of the code. Call me a purist, but I like to know exactly what I'm working with. Fewer errors as a result.

  14. #14
    Registered User
    Join Date
    Feb 2011
    Posts
    16
    aha I understand what you're saying.

    Ok I fixed the problem by creating a new funtion that initializes the linked list; adds the header node. But now it gave me the same error in another location.
    Here is the location of the error:
    Code:
    Position findPrevious(Position p, List src)
    {
        Position S;
        S = src;
        if(S != NULL)
        {
            while( S->next != NULL  && S->next!= p )    // Here is the location of error
            S=S->next;
        }
    
        return S;
    }
    The aim of this funtion is to find the previous node of the node we're working on.
    But why is it giving me the error again? As you can see I checked the pointer S if it's NULL before chkecking S->next.

  15. #15
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    There is nothing wrong with that code, however, you can still get a seg fault if S contains some other invalid address besides NULL (e.g. 0x1234). I would suspect that somewhere, your list is being built incorrectly or it's being corrupted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  2. SIGSEGV, Segmentation fault
    By micmac700 in forum C Programming
    Replies: 3
    Last Post: 12-13-2006, 03:47 PM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  4. debugging: sigsegv, segmentation fault???
    By Gonzo in forum C Programming
    Replies: 9
    Last Post: 09-16-2003, 06:56 AM
  5. SIGSEGV, segmentation fault
    By StuBarnes4Prez in forum C++ Programming
    Replies: 4
    Last Post: 01-04-2002, 09:26 PM