首页 文章

clang运行错误gcc ok

提问于
浏览
0
#ifndef _LIST_H
#define _LIST_H

typedef int element_type;
typedef struct node * p_node;
typedef p_node list;

typedef struct node {
    element_type e;
    p_node next;
}node;
#endif
list list_append(list l, element_type n) {
    p_node t = (p_node)malloc(sizeof(node));
    if (!t) {
        exit(1);
    }
    t->e = n;
    while(l->next) {
        l = l->next;
    }
    l->next = t;
    return l;
}
#include <stdio.h>
#include "list.h"


int main() {
    list l = list_init();
    list_append(l,3);
    return 0;
}

上面的c代码,它可以在gcc中运行,但不能在clang env中运行 . gcc版本:4.4.7 20120313(Red Hat 4.4.7-18)(GCC)clang:配置: - prefix = / Library / Developer / CommandLineTools / usr --with-gxx-include-dir = / usr / include / c /4.2.1 Apple LLVM版本8.1.0(clang-802.0.42)目标:x86_64-apple-darwin16.4.0线程模型:posix

当它在clang env中运行时,抛出Segmentation fault:11 .

1 回答

  • 0
    list list_init() {
        p_node t = (p_node)malloc(sizeof(node));
        if(!t) {
            exit(1);
        }
    
        return t;
    }
    

相关问题