0
votes

#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 code above, it can run in gcc, but can't run in clang env. gcc version :4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) clang: Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM version 8.1.0 (clang-802.0.42) Target: x86_64-apple-darwin16.4.0 Thread model: posix

when it run in clang env, throw Segmentation fault: 11.

1

1 Answers

0
votes

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

    return t;
}