list for each引起的問題

2022-03-14 12:18:05 字數 771 閱讀 8645

在linux裡核心通用列表中list_for_each經常被使用。但這裡有一點值得注意,最近在自己專案裡就碰到了。

list_for_each的定義如下:

#define list_for_each(pos, head) \

for(pos = (head)->next; pos != (head); pos = pos->next)

list_del的定義如下:

void list_del(struct list_head *entry)

__list_del(entry->entry->prev, entry->next);

entry->next = list_position1;

entry->prev = list_position2;

從定義中可以看到如果在list_for_each遍歷過程中使用list_del,會出問題。list_del後,pos的next,prev都已經變為null, pos = pos->next執行時就會異常訪問。

當在遍歷列表時,應該使用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)

typedef struct引起的結構體問題

原來一直認為結構體定義只有struct開頭的方法。今天看一本書突然看到typedef struct開頭的結構體定義方式吧自己搞的一臉懵逼。既然如此那就看看吧!1 先定義結構體型別,再定義變數 注 注意在c語言中定義結構體變數的時候是struct student打頭的。struct student s...

Linux核心中的list for each

在linux核心原始碼中,經常要對鍊錶進行操作,其中乙個很重要的巨集是list for each entry 意思大體如下 假設只有兩個結點,則第乙個member代表head,list for each entry的作用就是迴圈遍歷每乙個pos中的member子項。巨集list for each e...

inline block引起的空白間隙的問題

div class card con div class card shop item div div class card shop item div div class card shop item div div class card shop item div div 例如上述例子,外層ca...