作者d630200x (DOGE)
看板C_and_CPP
标题[问题] 为甚麽会segmentation fault?
时间Wed Oct 16 19:25:38 2019
int makeList(List_t* list)
{
list = malloc(sizeof(List_t));
if(!list){
return list_makeFail;
}else{
return list_success;
}
}
bool isEmpty(List* list)
{
return (list->head == NULL);
}
int main()
{
int ret;
List_t* testList;
ret = makeList(testList);
printf("make list is %d\n", ret);
ret = isEmpty(testList);
printf("list is empty : %s\n", ret ? "true":"false");
return 0;
}
测了一下是死在isEmpty()
另外试了一下:
List_t testList = malloc(sizeof(List_t));
isEmpty(testList);
这样是没问题的,我是哪边的观念有错误吗?
平台是centos7,用的是C
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 175.98.120.34 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1571225142.A.670.html
1F:推 ab4daa: makeList里面malloc的pointer没传出来 10/16 19:47
2F:→ ab4daa: 所以isEmpty一用就爆 10/16 19:47
3F:→ withoutshine: 没看到 List_t 的宣告,但我猜你需要的是 10/16 20:56
4F:→ withoutshine: a pointer to a pointer 10/16 20:56
5F:→ loveme00835: 因为爱 10/17 00:57
6F:推 Ryspon: 推三楼,另外可能也要复习一下 call by value 跟 pointer 10/17 14:19
7F:→ dces4212: 两种做法,不是用 ptr to ptr 就是回传 malloc 拿到的 10/17 20:58
8F:→ dces4212: 记忆体位置给 testList,你这实作比较适合用前者 10/17 20:58
9F:推 Gway: pass pointer 变数到fun中会是一个副本(I.e copy)你的 mak 10/18 16:50
10F:→ Gway: eList(List_t* list)中的list 不是你原本预期main里面的test 10/18 16:50
11F:→ Gway: list变数 10/18 16:50
12F:推 elysium5290: 在malloc後/ main内打印一下address就知道问题在哪了 02/28 15:31