[英]How do you fix a segmentation fault
When I run my code I keep getting a message that I have a segmentation fault. 当我运行代码时,我不断收到一条消息,指出我存在分段错误。 I did a core dump with the GDB and it told me that my problem is with p = p->next;
我对GDB进行了一次核心转储,它告诉我问题出在p = p-> next; int the function below.
int下面的函数。 Can someone help me understand what I did wrong and how I can go about fixing it?
有人可以帮助我了解我做错了什么以及如何解决该问题? I know that segmentation faults have something to do with incorrect use of pointers but I am not sure what is wrong with p = p->next.
我知道分段错误与不正确使用指针有关,但是我不确定p = p-> next有什么问题。
int list_size(const list_t *h) {
node_t *p = *h;
int r = 0;
do {
r += 1;
p = p->next;
} while (p);
return r;
}
Check for null pointers before you use them. 在使用空指针之前,请先检查它们。
Change 更改
do {
r += 1;
p = p->next;
} while (p);
to 至
while (p)
{
r += 1;
p = p->next;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.