Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a problem in the linklist.c #2

Open
bunnerlemon opened this issue Feb 5, 2021 · 0 comments
Open

a problem in the linklist.c #2

bunnerlemon opened this issue Feb 5, 2021 · 0 comments

Comments

@bunnerlemon
Copy link

I encountered a dangling pointer problem in the function r_utils_linklist_pop when I implemented the project.

void* r_utils_list_pop(r_utils_linklist_s *l) {
  r_utils_linklist_cell_s *c;
  void *elem;

  assert(l != NULL);

  if(l->head == NULL)
    return NULL;

  c = l->tail;
  elem = c->elem;
  l->tail = c->prev;

  if(l->tail == NULL)
    l->head = NULL;

  free(c);
  l->num--;

  return elem;
}

And I think I should set l->tail->next = NULL if l->tail != NULL when I pop an element.

if (l->tail == NULL) l->head = NULL;
else l->tail->next = NULL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant