Here is my list struct:
typedef struct Header {
  struct Header* next;
  char empty;
  int storageSize;
} Header;
And I am looping over a list to check how many elements its has:
int listSize(Header* _header) {
  int count = 0;
  while(_header->next) {
    _header = _header->next;
    ++count;
  }
  return count;
}
And I get a segfault after reaching the end of the list. However if I change it to:
int listSize(Header* _header) {
  int count = 0;
  while(_header->next) {
    if(!_header->next) {
      _header = _header->next;
      ++count;
    }
    else
      break;
  }
  return count;
}
It doesn't segfault, but it also obviously doesn't count the elements right.
 
     
     
     
    