【问题标题】:Why is my program terminating abruptly?为什么我的程序突然终止?
【发布时间】:2013-12-30 18:53:04
【问题描述】:

我有一个创建单链表的程序,在列表的开头和结尾添加一个节点。该程序正在产生预期的输出,但在产生最后一个输出后崩溃。我在 main() 中创建了链表,我有 2 个函数分别在开头和结尾添加一个节点。

#include<stdio.h>
struct book
{

  char bname[20];
  char aname[20];
  int pages;
  struct book *next;
};


struct book *first;
struct book *current;
struct book *previous;
int main()
{
  int count=0,temp=0;


  for(int i=1;i<4;i++)
  {

    current=(struct book*)malloc(sizeof(struct book));//memory assigned to only the current structure
    if(current==NULL)
    {
      break;
    }
    if(first==NULL)
    {
      first=current; //if the first node value is null then it hadn't yet been processed,so current node is now the first;. 
    }
    if(previous!=NULL)
    {
      previous->next=current;//stores the current structure address to the next member(pointer) of the previous structure address
    }

    printf("\nBook Name:: ");
    scanf("%s",current->bname);

    printf("\nAuthor Name:: ");
    scanf("%s",current->aname);

    printf("\nPages::");
    scanf("%d",&current->pages);

    current->next=NULL;//if this is the last node
    //will again be filled up if there is a next structure

    previous=current;//the current node is the previous node for the next iteration 
  }


  current=first;
  while(current!=NULL)
  {
    count++;
    printf("\nBook Name:: %s Author Name:: %s Pages:: %d",current->bname,current->aname,current->pages);
    previous=current;//store to previous only because of freeing the current structure
    current=current->next;//next structure address of the current node is the current node of the next iteration.Now if this next address doesn't exist,then the current pointer does not exist and the loop terminates


  }
  addnodebeginning(first);

}


/*****************************inserting at the BEGINNING*********************************/

void addnodebeginning(struct book *first)
{
  int count=0,temp=0;
  current=(struct book*)malloc(sizeof(struct book));
  current->next=first;
  first=current;
  printf("\nBook Name:: ");
  scanf("%s",current->bname);

  printf("\nAuthor Name:: ");
  scanf("%s",current->aname);

  printf("\nPages::");
  scanf("%d",&current->pages);


  current=first;
  while(current!=NULL)
  {
    count++;
    printf("\nBook Name:: %s Author Name:: %s Pages:: %d",current->bname,current->aname,current->pages);
    previous=current;//store to previous only because of freeing the current structure
    current=current->next;//next structure address of the current node is the current node of the next iteration.Now if this next address doesn't exist,then the current pointer does not exist and the loop terminates


  }

  addnode_end(first);

}
/*****************************inserting at the end*********************************/

void addnode_end(struct node *first)
{

  current=first;

  while(1)
  {

    if(current->next==NULL)
    {
      struct book *newnode=(struct book*)malloc(sizeof(struct book));

      printf("\nBook Name:: ");
      scanf("%s",newnode->bname);

      printf("\nAuthor Name:: ");
      scanf("%s",newnode->aname);

      printf("\nPages::");
      scanf("%d",&newnode->pages);

      current->next=newnode;
      //newnode->next=NULL;
      break;
    }
    current=current->next;
  }

  current=first;
  while(current!=NULL)
  {

    printf("\nBook Name:: %s Author Name:: %s Pages:: %d",current->bname,current->aname,current->pages);
    previous=current;//store to previous only because of freeing the current structure
    current=current->next;
    free(previous);

  }

}

为什么会这样?

【问题讨论】:

  • 我想知道这个编译器是否正确!
  • 你的函数原型丢失了。
  • 是的,我可以向你保证它编译得非常好!已经测试了不止一次。
  • @Digital_Reality 它在 gcc 上以 c99 模式编译,但有 17 个警告(有些严重)

标签: c data-structures linked-list singly-linked-list


【解决方案1】:

addnodebeginning 函数中,first 参数按值 传递,这意味着该值是复制 并且您在函数中修改副本。如果你想修改传递的值,你必须通过引用传递它

这是通过传递一个指向值的指针来完成的,在本例中是指向指针的指针。

【讨论】:

  • 你能告诉我确切的代码吗?我对指针有点陌生,上面的程序完全基于我的理解。我很乐意按照你说的方式来做。帮我解决这个问题。
【解决方案2】:

首先,请注意警告。他们在那里是有原因的。其次,您只需将其包含在 if 测试的末尾 -

newnode->next=NULL;
 break;

您的程序将不会出现运行时崩溃。

【讨论】:

  • 非常感谢..很想投票给你,但我的声誉还不够。
【解决方案3】:

代码错过了#include&lt;stdlib.h&gt;。至少在 64 位系统上调用 malloc() 很可能是致命的。

如果在 64 位系统上并且缺少 malloc() 的原型,编译器会假定 int 作为 malloc() 的返回值。 int 肯定是 32 位的,而 malloc() 尝试返回的地址是 64 位宽,所以返回的地址值很可能在返回时被截断一半。


还有这一行

void addnode_end(struct node *first)

应该是

void addnode_end(struct book *first)

更新

此外,代码也错过了初始化一个新的malloc()ed 节点的成员。通常,其next 成员应显式设置为NULL

【讨论】:

  • -1,这会导致编译错误,而不是运行时错误/崩溃。
  • @dvnrrs:不,这取决于编译器及其设置。我的 gcc 只是吞下了上面的所有来源,没有任何错误。是的,它发出了很多警告。请重新检查您所说的内容。
  • 它没有导致任何编译错误。但是我已经更改了它。仍然是同样的问题。
  • @user3130420:您可能希望使用符号编译(-g 用于 gcc)并使用 gdb 运行代码。当它崩溃时,发出bt 命令并查看堆栈跟踪,显示它是否崩溃以及它是如何到达那里的。同时编译所有警告(-Wall -Wextra -pedantic)并修复代码,直到不再出现警告。
  • @alk:我已经纠正了这两个错误。问题依然存在。请问先生该怎么办?
猜你喜欢
  • 2019-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-20
  • 1970-01-01
  • 2016-06-30
相关资源
最近更新 更多