【问题标题】:Show " Segmentation fault " on cygwin在 cygwin 上显示“分段错误”
【发布时间】:2015-12-13 02:04:47
【问题描述】:

我用的是cygwin,安装了gcc-g++来编译用notepad++写的c。

我想创建一个链表来存储数据,但是总是报“Segmentation fault (core dumped)”

我发现如果我将“printf(“OK”)”放在主函数第 4 行的“scanf”之后,它不起作用,但会再次显示分段错误。

这是内存管理问题吗?还是指针滥用?

这是我的代码:

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

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

typedef struct Node *nodePointer;


nodePointer GetNewNode(){           // create a new node
    nodePointer NewNode;    
    NewNode = (nodePointer) malloc(sizeof(node)); 
    if (NewNode == NULL)
        printf("memery is not enough");
    return NewNode;
}

nodePointer insert(nodePointer ptr,nodePointer L,int NewData){  
    nodePointer NewNode,trace;
    NewNode = GetNewNode();
    if(ptr==NULL){                  //to insert first Node
        ptr->link = NewNode;
        NewNode->link = NULL;
        NewNode->data = NewData;
        L = ptr;
    }
    else{                           //after insert first Node,insert next Node
        trace = L;
        while(trace->link!=NULL) trace = trace->link;
        trace->link = NewNode;
        NewNode->data = NewData;
    }
    return L;
}

 int main(){
    int data;
    nodePointer ptr=NULL,L=NULL,visit=NULL;
    printf("input data to storage, input 0 to print data\n");
    scanf("%d",&data);
    while(data!=0){                 // input data to store, input 0 to print data
        L = insert(ptr,L,data);
        scanf("%d",&data);
    }
    visit = L;
    while(visit->link!=NULL)
        printf("%d",visit->data);
}

【问题讨论】:

  • 未定义行为未定义.. 使用调试器(gdb 等)来识别问题并发布详细信息
  • typedef struct Node *nodePointer; 永远不要在 typedef 后面隐藏指针,这是一种不好的风格,因为它会使程序更难阅读和理解。
  • 我明白了,谢谢你们的指导!
  • 除了愚蠢的 typedef:... = malloc(sizeof(node)); 巧合(除非 OP 使用 C++ 编译器,他不应该这样做)
  • 使用gcc 编译C,使用g++ 编译C++。它们是不同的语言。

标签: c segmentation-fault


【解决方案1】:

在你的代码中你有这个:

if (ptr==NULL){
    // ptr is NULL here and right after you dereference ptr
    // so it's normal that you get a segfault
    ptr->link = NewNode;

这里可能还有更多问题。

【讨论】:

    【解决方案2】:

    改正后,工作顺利。

    #include <stdio.h>
    #include <malloc.h>
    
    typedef struct Node *nodePointer;
    struct Node{
        int data;
        nodePointer link;
    }node;
    
    nodePointer GetNewNode(){
        nodePointer NewNode;    
        NewNode = (nodePointer) malloc(sizeof(node));  
        if (NewNode == NULL)
            printf("memery is not enough");
        return NewNode;
    }
    
    nodePointer insert(nodePointer L,int NewData){
        nodePointer NewNode,trace;
        if(L==NULL){                
            L = (nodePointer) malloc(sizeof(node));
            L->data = NewData;
            L->link = NULL;
        }
        else{                       
            malloc(sizeof(node)); there no necessaryry
            trace = L;
            while(trace->link!=NULL) trace = trace->link;
            NewNode = GetNewNode();
            trace->link = NewNode;
            NewNode->data = NewData;
            NewNode->link = NULL;
        }
        return L;
    }
    
     int main(){
        int data;
        nodePointer L=NULL,visit=NULL;
        printf("input data to storage, input -1 to print data\n");
        while(data!=-1){                
            scanf("%d",&data);
            if(data==1) break;
            L = insert(L,data);
        }                           
        visit = L;
        while(visit!=NULL){
            printf("%d",visit->data);
            visit = visit->link;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 2014-08-15
      • 2020-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-08
      相关资源
      最近更新 更多