0

I am trying to add a node to the end of a singly linked list but am getting a segmentation fault (core dumped error)

void slist_add_back(struct slist *l, char *str) {
    struct snode *temp;

    do {
        l->front = l->front->next;
        l->counter++;
    } while (l->front !=NULL);

    l->counter++;
    temp = (struct snode *)malloc(sizeof(struct snode));
    l->back = l->front;
    l->back->next = temp;
    l->back = temp;
}
8
  • You seem to be incrementing the counter quite a bit for only adding one element. Commented Jan 29, 2014 at 2:01
  • This is more C code than it is C++. Commented Jan 29, 2014 at 2:08
  • 2
    Does front represent the front of your list? If so, you don't want to be changing it just because you're adding another element here. Commented Jan 29, 2014 at 2:08
  • yes front represent the front of my list but I am trying to traverse the list so I can enter in the new node? Commented Jan 29, 2014 at 2:10
  • 2
    this code makes zero sense. I've found about 10 bugs. I'm not kidding. Commented Jan 29, 2014 at 2:11

4 Answers 4

1

When you write:

do{
      l->front = l->front->next;
      l->counter++;

   }while(l->front !=NULL);

At the end l->front is null. Now l->back = l->front; implies l->back is also null. So this assignment is wrong:

l->back->next = temp; // causing segfault
Sign up to request clarification or add additional context in comments.

Comments

0

One of your problems lies here:

do
{
    l->front = l->front->next;
    l->counter++;
} while(l->front !=NULL);

You are modifying the list instead of iterating it. It should be something like this:

snode* curr = l->front;
if (curr == NULL) // first element
{
    front = new snode(str);
    return;
}

while (curr->next != NULL)
{
    ++curr;
}
curr->next = new snode(str); // or the malloc version if you want to stay in C

2 Comments

this is C, there's no new.
@KarolyHorvath His original tag was C++ (hence my comment both on the OP and in the code example).
0

I guess this is something what you want? I dont really understand what your code is doing, sorry.

void slist_add_back(struct slist *l, char *str)
{


   struct snode *currentNode = l->front;
   if(currentNode!=NULL)
   {
      struct snode *temp = (struct snode *)malloc(sizeof(struct snode));
      while(currentNode->next!=NULL)
      {
         l->counter++;//although i dont know why
         currentNode = currentNode->next;
      }


      l->counter++;//again
      currentNode->next = temp;
      l->back = temp;
      temp->next = NULL;
   }
}

Comments

0

Here's how your code roughly should look like:

void slist_add_back(struct slist *l, char *str)
{
    struct snode *temp = malloc(sizeof(struct snode));
    temp->next = NULL;
    //note: store str... (strdup?)

    if (! l->back) {
        l->front = temp;
    } else {
        l->back->next = temp;
    }
    l->back = temp;
    l->counter++;
}

Note: Based on your code I would be really surprised if the rest wouldn't be full of bugs, so except crashes even if this part is fixed...

Pick a debugger, and check what your code really does.

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.