Tag: foreach

为什么我们需要list_for_each_safe()来删除内核链表中的节点?

我正在学习如何使用list.h中的内核链表API。 我了解到,当使用list_del()而不是使用list_for_each()删除节点时,我需要使用list_for_each() 。 list_for_each_safe()代码: #define list_for_each_safe(pos, n, head) \ for (pos = (head)->next, n = pos->next; pos != (head); \ pos = n, n = pos->next) list_for_each()代码: for (pos = (head)->next; pos != (head); pos = pos->next) 我注意到它们都非常相似,只是_safe版本需要一个额外的参数用作’临时存储’(这里说明, list.h )。 我知道何时应用相应的function, _safe版本用于删除,正常版本用于访问,但我很好奇额外的参数如何使其“安全”? 请考虑以下内容,我使用list_for_each_safe()删除链表中的每个节点: struct kool_list{ int to; struct list_head list; int from; }; struct kool_list […]