排除链接列表中的分段故障(核心转储)故障

时间:2015-04-19 23:27:44

标签: function debugging pointers linked-list segmentation-fault

Node *orderedInsert(Node *p, int newval)
/* Allocates a new Node with data value newval
   and inserts into the ordered list with 
   first node pointer p in such a way that the
   data values in the modified list are in 
   nondecreasing order as the list is traversed.
*/
{
   Node* current = NULL;
   Node* prev = NULL;
   Node* newNode = (Node*)malloc(sizeof(Node));
   newNode->next = NULL;
   newNode->data = newval;

   if(newNode == NULL)
      printf("Could not allocate memory for new node");

   current = p;   

   if(p == NULL){

      current = newNode;
      newNode->next = NULL;
      newNode->data = newval;
      return  newNode;
   }
   else if(newval < p->data){
      newNode->next = p;
      p = newNode;
      return p;
   }
   else{
      prev = p;
      current = current->next;   

      while(newNode->data > current->data && current != NULL){

         prev = current;
         current = current->next;
      }
      if(current == NULL){
         prev->next = newNode;
         newNode->next = NULL;
      }
      else{
         newNode->next = current;
         prev->next = newNode;
      }
   }

}

void printList(FILE *outfile, Node *p)
/* Prints the data values in the list with 
   first node pointer p from first to last,
   with a space between successive values.
   Prints a newline at the end of the list.
*/
{

   Node* current = p;

   while(current != NULL){
      fprintf(outfile, "%d ",current->data);
      current = current->next;
   }
   fprintf(outfile, "\n");

}

int main(int argc, char * argv[])
{
   assert(argc == 3);
   Node * p = NULL;
   int newval, retval;
   int op;

   FILE *in = fopen(argv[1],"r");
   assert(in != NULL);
   FILE *out = fopen(argv[2],"w");
   assert(out != NULL);
   do {
      op = fgetc(in);
   }  while (op != EOF && isspace(op));

   while(op != EOF && op != 'q') {
      switch(op) {
     case 'i':
        fscanf(in,"%d",&newval);
        p = orderedInsert(p,newval);
        printList(out,p);
        printList(stdout,p);
        break;
     case 'c':
        clearList(&p);
        break;
     default:
        fclose(in);
        fclose(out);
        return 0;
      }
      do
     op = fgetc(in);
      while (op != EOF && isspace(op));
   }

   fclose(in);
   fclose(out);
   return 0;
}

我在使用此错误调试代码时遇到问题。我的代码中是否有一些明显的缺失和/或您是否有任何调试此错误的提示?我只是发现自己失去了从哪里开始,除了它甚至没有超过第一个列表条目(当列表为空时)。

由于

编辑:我已经修改了代码,现在只输入大于列表中第一个的数字时出现分段错误。

1 个答案:

答案 0 :(得分:1)

添加一些关于调试C / C ++程序的一般说明。

GDB是一个很好的工具,您必须将它附加到您的可执行文件,然后运行该程序。没有gui模式,但人们围绕它建立了gui包装器,例如DDD

我发现如果可以的话,使用IDE会更容易,具体取决于您使用MS Visual Studio的环境,Netbeans CPPEclipse CDTQtCreator以及Jetbrains的最新C ++ IDE { {3}}应该是一些可以帮助您更好地编码的工具。

测试一下,它不会对我造成分段错误。我正在使用g ++版本(Ubuntu 4.8.2-19ubuntu1) 4.8.2在ubuntu 14.04上进行测试。逻辑错误可能存在,因为我不太清楚从file1读取然后插入文件2的逻辑

编译:{{1​​}}

运行:g++ Node.cpp

./a.out /home/vsnyc/tmp/1.txt /home/vsnyc/tmp/2.txt

Node.cpp如下:

File: 1.txt
i 12 i 4 i 6 i 9 q