【问题标题】:clang run error gcc okclang 运行错误 gcc ok
【发布时间】:2018-03-09 20:53:39
【问题描述】:

#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(红帽 4.4.7-18)(GCC) 铛: 配置: --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.

【问题讨论】:

    标签: clang gcc4


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-23
      • 2021-07-06
      • 1970-01-01
      • 2017-10-17
      • 2023-03-29
      • 1970-01-01
      • 2014-02-27
      • 2012-12-04
      相关资源
      最近更新 更多