Our original writing method was to point the pointer to the node structure. Linus's second writing method was to point the pointer to the next pointer in the node structure.
Abstract:Linus Torvalds admitted that the spoofed kernel "demons" he liked by searching for high-speed cache through file names and then complaining about his ability. On the contrary, many people haven't even learned well about low-level kernel programming.
A few weeks ago, Linus Torvalds answered some questions on Slashdot. One of them aroused the strong attention of developers. When asked about the kernel hacker in his mind, he said that he had not read the code these days unless he helped others review the code. He paused for a moment and admitted that the "sly" kernel "(hackers) who searched for the high-speed cache through file names and complained about their average capabilities were what he liked.
He said:
On the contrary, many people are not even familiar with low-level kernel programming. For example, the Name Lookup function of lockless is not complex, but it is a simple and good way to use pointers. For example, I have seen many people delete a list item of One-Way link by tracking the previous page entry, and then Delete this entry. For example:
- if (prev)
- prev->next = entry->next;
- else
- list_head = entry->next;
Whenever I see the code, I will say, "This person doesn't know the pointer ". This is still a sad and common problem.
If the developer can understand the pointer, he only needs to use "pointer pointing to this entry" and initialize list_head, and then run through the list. In this case, the entry can be deleted without any conditional statements, you only need to use * PP = entry-> next.
I think I understand pointers, but unfortunately, if you want to implement the delete function, I will keep track of the previous list nodes. Here is the draft code:
Practices of a person who does not understand pointers:
- typedef struct node
- {
- struct node * next;
- ....
- } node;
-
- typedef bool (* remove_fn)(node const * v);
-
- // Remove all nodes from the supplied list for which the
- // supplied remove function returns true.
- // Returns the new head of the list.
- node * remove_if(node * head, remove_fn rm)
- {
- for (node * prev = NULL, * curr = head; curr != NULL; )
- {
- node * next = curr->next;
- if (rm(curr))
- {
- if (prev)
- prev->next = curr->next;
- else
- head = curr->next;
- free(curr);
- }
- else
- prev = curr;
- curr = next;
- }
- return head;
- }
This linked list is very simple, but the pointer and sentinel value of each node can be built into a perfect structure, but the code for modifying this table needs to be very subtle. It's no wonder that the linked list feature will often appear in many interview sessions.
The Code executed above is the condition required to delete any node from the list header.
Now, let's remember how Linus Torvalds executes the code. In this case, we use a pointer to point to the list header to traverse and modify the list.
Two Star programming:
- void remove_if(node ** head, remove_fn rm)
- {
- for (node** curr = head; *curr; )
- {
- node * entry = *curr;
- if (rm(entry))
- {
- *curr = entry->next;
- free(entry);
- }
- else
- curr = &entry->next;
- }
- }
Much better! The most important part is that links in the linked list are pointers. Therefore, pointers to pointers are the first choice for modifying the linked list.
The original version of remove_if () is an example of using double asterisks. The double asterisks represent two indirect addressing, and adding a star will be too redundant.