1

I'm trying to make an array of nodes,and enter values from S[] array to it. But I keep getting a segmentation fault. My struct looks like this:

typedef struct Node {
int num;
struct Node *next;
}ListNode;

Initialized array of S[10] with random numbers:

    printf("list is:");

    for(i=0;i<10; i++)
    {
            RN= (random()+1);
            S[i]=RN;
            printf("%d  ",S[i]);
    }
    printf("\n");

Here is how I initialized my array of nodes:

    ListNode *bucket[Radix];
    for(j=0; j<Radix; j++)
      {
            bucket[i]=(ListNode*)malloc(sizeof(ListNode));
            bucket[i]->next=NULL;
      }

And this is the function I use to read array numbers from S[] into the array of linked list, bucket[]:

     for(y=0;y<(sizeof(S)/sizeof(int));y++) // S is size of a normal array
        {
            digit=bucketNumber(S[y],1);// returns the first digit values
            pointer= bucket[digit];
            number= S[y];
            insert_tail(pointer, number);
        }

my insert_tail function look like this:

    ListNode *insert_tail(ListNode *tail, int data)
    // insert and element at tail

    {

    if (tail==NULL)
    { tail=(ListNode *)malloc(sizeof(ListNode));}

    else
    {
        while(tail->next !=NULL)

        tail->next = (ListNode *)malloc(sizeof(ListNode));
        tail= tail->next;
    }

    tail->num= data;
    tail->next=NULL;
    }

this is the bucketNumber function:

    int bucketNumber(int num,int digit)
    {
    int x, y;
    y= 10*digit;
    x= num%y;

    if(digit>=2)
    {
      num= num%y;
      x= num/(y/10);
    }
    return (x);

}

I think the reason for segmentation fault is that my function is not creating the links in the array properly. I'm not sure thou, there could be something else wrong!

5
  • 1
    Run your program in a debugger. Commented Nov 7, 2015 at 21:02
  • To slightly expand upon kaylum's point, it's usually trivial to run the program using a debugger such that when the seg fault occurs, you can see exactly what line it occurs on. Given that plus that you may or may not be showing all of the relevant code (despite a good effort there), people may not be able to help you. Post a comment with your development environment details if you need help identifying the seg-fault line. Commented Nov 7, 2015 at 21:34
  • I'm working on zeus. Plus, I was just using the debugger, and it pointed out an error at insert_tail function. I tried fixing it, but its still not working. Commented Nov 7, 2015 at 21:47
  • I'm not familiar with that IDE. At a glance, it doesn't look like they post their manual on their website, so hopefully you have built-in help or a provided PDF that is helpful. Commented Nov 7, 2015 at 21:58
  • ...Actually, I think I see the problem. I'll post as an answer shortly and hopefully it helps. Commented Nov 7, 2015 at 22:00

1 Answer 1

2

I think the problem is in this section of insert_tail():

else
    {
        while(tail->next !=NULL)

        tail->next = (ListNode *)malloc(sizeof(ListNode));
        tail= tail->next;
    }

    tail->num= data;
    tail->next=NULL;

Since the while() loop doesn't have curly braces following it, the below line gets executed if and only if tail->next != NULL:

tail->next = (ListNode *)malloc(sizeof(ListNode));

...which is sort of the opposite of what you want; you want to allocate a new node for next if next is NULL. As is, next is likely NULL, and so tail gets moved forward to next, but next is not allocated--it's NULL. The bottom two lines above would each cause a segmentation fault in that case, since you can't dereference a NULL pointer, which is what -> is doing.

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

1 Comment

oh I see! Thank you. :)

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.