在_IO_vfscanf_internal()中接收SIGSEGV

时间:2016-11-19 00:01:52

标签: c linux c89

我的程序将编译并正常运行,但是,当我通过GDB运行程序时,我收到一个SIGSEGV,Segmentation fault (来自/lib64/libc.so.6的_IO_vfscanf_internal()中的0x0000003ec945352f) 在我的代码中的fscanf填充链表的节点之后。是什么原因导致GDB中的分段错误,而不是在我运行程序时?

(gdb) s
90          fscanf(fp,"%s %s %d %d", newNode->record_->first_name_, newNode->record_->last_name_, &newNode->record_->student_id_, &newNode->record_->student_age_);
(gdb) s

Program received signal SIGSEGV, Segmentation fault.
0x0000003ec945352f in _IO_vfscanf_internal () from /lib64/libc.so.6
#include <stdio.h>
#include <stdlib.h>

struct student_record_node
{
  struct student_record* record_;
  struct student_record_node* next_;
};
struct student_record
{
  int student_id_;
  int student_age_;
  char first_name_[21];
  char last_name_[21];
};

void parseFile();
struct student_record_node* student_record_allocate();
void appendNode(struct student_record_node* head, struct student_record_node* newNode);
void printNode(struct student_record_node* node);
void student_record_node_deallocate(struct student_record_node* node);
void printNodeList(struct student_record_node* head);
void swap(struct student_record_node** node1, struct student_record_node** node2);
void sortByAge(struct student_record_node** recordsHead);
void sortById(struct student_record_node** recordsHead);
void freeNodeList(struct student_record_node* head);
void student_record_node_deallocate(struct student_record_node* node);


int main(int argc, char* argv[])
{
  struct student_record_node* node = student_record_allocate();
  struct student_record_node** head = &node;
  parseFile(argv[1], head);
  printf("Before sorting...\n");
  printNodeList(*head);
  sortByAge(head);
  printf("Sorting by age...\n");
  printNodeList(*head);
  sortById(head);
  printf("Sorting by id...\n");
  printNodeList(*head);
  freeNodeList(*head);
}


struct student_record_node* student_record_allocate()
{
   struct student_record_node* newNode;
  newNode = calloc(1, sizeof(struct student_record_node));
  if (newNode == NULL)
  {
    printf("No memory allocated for newNode");
    exit(1);
  }

  newNode->record_ = calloc(1, sizeof(struct student_record));
if (newNode->record_ == NULL)
  {
    printf("No memory allocated for record_");
    exit(1);
  }
  newNode->next_ = NULL;
  return newNode;
}

void parseFile(char* filename, struct student_record_node** head)
{
  int i = 0;
  struct student_record_node* newNode = *head;
  FILE* fp;
  fp = fopen(filename,"r");
  if (fp == NULL)
  {
    printf("Could not read file\n");
    exit(1);
  }

  while(1)
  {
    if(newNode != *head)
    {
      newNode = student_record_allocate();
      appendNode(*head, newNode);
    }


    fscanf(fp,"%s %s %d %d", newNode->record_->first_name_, newNode->record_->last_name_, &newNode->record_->student_id_, &newNode->record_->student_age_);

    if(newNode == *head)
    {
      newNode = NULL;
    }
    if(feof(fp))
    {
      break;
    }
  }
  fclose(fp);
}

void appendNode(struct student_record_node* head, struct student_record_node* newNode)
{
  int i = 1;
  struct student_record_node* current = head;

  while (i)
  {
    if (current->next_ == NULL)
    {
      current->next_ = newNode;
      i = 0;
    }
    current = current->next_;
  }
}

void printNode(struct student_record_node* node)
{
  printf("struct student_record_node:\n");
  printf("\tStudent first name: %s\n", node->record_->first_name_);
  printf("\tStudent last name: %s\n", node->record_->last_name_);
  printf("\tStudent id: %d\n", node->record_->student_id_);
  printf("\tStudent age: %d\n", node->record_->student_age_);
}

void printNodeList(struct student_record_node* head)
{
  struct student_record_node* current = head;
  while (current->next_ != NULL)
  {
    printNode(current);
    current = current->next_;
  }
  printf("\n");
}

void swap(struct student_record_node** node1, struct student_record_node** node2)
{
  struct student_record* newNode;
  struct student_record_node* big = *node2;
  struct student_record_node* small = *node1;

  newNode = big->record_;
  big->record_ = small->record_;
  small->record_ = newNode;
}

void sortByAge(struct student_record_node** recordsHead)
{
  int i,j;

  struct student_record_node* current = *recordsHead;
  struct student_record_node* next = *recordsHead;
  while (current->next_->next_ != NULL)
  {
    next = current;
    while (next->next_->next_ != NULL)
    {
      if (next->next_->record_->student_age_ <= next->record_->student_age_)
      {
        swap(&next->next_, &next );
      }
    next = next->next_;
    }
  current = current->next_;
  }
}

void sortById(struct student_record_node** recordsHead)
{
  int i = 0;
  struct student_record_node* current = *recordsHead;
  struct student_record_node* beginning = *recordsHead;
  struct student_record_node* next = *recordsHead;

  while (current->next_->next_ != NULL)
  {
  next = beginning;
    while(next->next_->next_ !=NULL)
    {
      if (next->next_->record_->student_id_ <= next->record_->student_id_)
      {
        swap(&next->next_, &next);
      }
      next = next->next_;
    }
   current = current->next_;
  }
}

void student_record_node_deallocate(struct student_record_node* node)
{
  struct student_record_node* tmp;
  tmp = node;
  free(tmp->record_);
  free(tmp);
}

void freeNodeList(struct student_record_node* head)
{
  while (head != NULL)
  {
    student_record_node_deallocate(head);
    head = head->next_;
  }
}

0 个答案:

没有答案