Conversation Linus Torvalds: Most hackers do not even understand the pointer.

Source: Internet
Author: User
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:

  
  
  1. if (prev)  
  2.     prev->next = entry->next;  
  3. else  
  4.     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:

 
 
  1. typedef struct node  
  2. {  
  3.     struct node * next;  
  4.     ....  
  5. } node;  
  6.  
  7. typedef bool (* remove_fn)(node const * v);  
  8.  
  9. // Remove all nodes from the supplied list for which the   
  10. // supplied remove function returns true.  
  11. // Returns the new head of the list.  
  12. node * remove_if(node * head, remove_fn rm)  
  13. {  
  14.     for (node * prev = NULL, * curr = head; curr != NULL; )  
  15.     {  
  16.         node * next = curr->next;  
  17.         if (rm(curr))  
  18.         {  
  19.             if (prev)  
  20.                 prev->next = curr->next;  
  21.             else  
  22.                 head = curr->next;  
  23.             free(curr);  
  24.         }  
  25.         else  
  26.             prev = curr;  
  27.         curr = next;  
  28.     }  
  29.     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:

 
 
  1. void remove_if(node ** head, remove_fn rm)  
  2. {  
  3.     for (node** curr = head; *curr; )  
  4.     {  
  5.         node * entry = *curr;  
  6.         if (rm(entry))  
  7.         {  
  8.             *curr = entry->next;  
  9.             free(entry);  
  10.         }  
  11.         else  
  12.             curr = &entry->next;  
  13.     }  

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.