【问题标题】:segmentation error in ubuntuubuntu中的分段错误
【发布时间】:2015-08-16 09:14:12
【问题描述】:

请告诉我为什么我的程序中存在分段错误,没有错误。 我也尝试调试它,但它从未进入 for 语句。

#include<stdio.h>
#include<malloc.h>

struct node
    {
        int data;
        struct node* link;
    } *start;

main()
{ 
    int i,n,m;
    start=NULL;

    printf("enter the number of nodes you want");
    scanf("%d",&n);

    for(i=0;i<n;i++)
        {
            printf("enter the element you want to insert");
            scanf("%d",&m);
            create_list(m);
        }
}

create_list(int data)
{ 
    struct node *q,*temp;
    temp=(struct node *)malloc(sizeof(struct node));
    temp->data=data;
    temp->link=NULL;

    if(start==NULL)
        start=temp;
    else
        {
        while(q->link!=NULL) q=q->link;
        q->link=temp;
        }
}

【问题讨论】:

  • 它主要是一个坏指针
  • in create_list, q 在使用时未初始化。
  • 您是否尝试过使用调试器至少找出哪条线路崩溃?
  • 你能指出来吗?@SaeidYazdani
  • 我怀疑第 13 行崩溃,这是 scanf 调用,这并没有错。另外,请学习如何格式化和缩进你的代码,它或多或少是不可读的。

标签: c ubuntu segmentation-fault


【解决方案1】:

你忘记在使用之前初始化q

q = start;
while(q->link!=NULL) 

【讨论】:

    【解决方案2】:

    1.你还没有在create_list()中初始化q并使用它-

    while(q->link!=NULL)
    

    在此循环之前初始化q=start;

    2.还有free在函数中为temp分配的内存。

    3.main() 应该是int main(void)create_list 的类型是什么?在main之前声明它的原型。

    【讨论】:

      【解决方案3】:

      在函数create_list本地指针q没有被初始化

      struct node *q,*temp;
                  ^^^
      

      不过是循环访问的

      while(q->link!=NULL)
      

      我认为你的意思是以下

      else
      {
          q = start;
          while ( q->link != NULL ) q = q->link;
      
          q->link = temp;
      }
      

      考虑到函数应该在使用前声明。例如,将它声明放在 main 之前。其返回类型应为void 并明确指定。函数 main 也应具有返回类型 int

      例如

      void create_list( int data );
      
      int main( void )
      {
          //...
      

      在退出程序之前释放所有动态分配的内存是个好主意。

      另外,标头 &lt;malloc.h&gt; 不是标准的 C 标头。你应该改用&lt;stdlib.h&gt;

      【讨论】:

        【解决方案4】:

        在上述所有建议之后,如果您尝试展示您清晰编码的一些工作并尊重最低标准,那么将来会很明显。

        这是查看代码的一种方式:

        #include<stdio.h>
        #include<stdlib.h> /* You need stdlib not malloc */
        
        void create_list(int data); /* If you don't declare your function the compiler doesn't know nothing about create_list */
        
        struct node{
            int data;
            struct node* link;
        }*start;
        
        int main(void){  /* Here return type of main is int and if no arg needed should be used void */
            int i,n,m;
            start=NULL;
            printf("enter the number of nodes you want");
            if((scanf("%d",&n)) != EOF)  /* always check scanf's return */
        
            for(i=0;i<n;i++){
                printf("enter the element you want to insert");
                if((scanf("%d",&m)) != EOF)   /* here the same: always check scanf's return */
                create_list(m);
            }
        
            return 0; /* return of main should be 0 or one of the following: EXIT_SUCCESS or EXIT_FAILURE, but 0 will be ok because it is standard*/
        }
        
        void create_list(int data){  /* here should be explicit what kind of function is */
            struct node *q,*temp;
            temp=(struct node *)malloc(sizeof(struct node)); /* if you allocate memory dynamically ..... */
            temp->data=data;
            temp->link=NULL;
        
            if(start==NULL){
                start=temp;
            }else{
                q = start;
                while(q->link!=NULL){
                    q=q->link;
                    q->link=temp;
                }
            }
        
            free(temp);  /* .....then always free it */
        }
        

        【讨论】:

          猜你喜欢
          • 2011-10-07
          • 2018-07-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-12-18
          • 2021-11-01
          • 2017-09-21
          • 1970-01-01
          相关资源
          最近更新 更多