linked-list-good-taste | Linus Torvalds' linked list argument for good taste, explained
kandi X-RAY | linked-list-good-taste Summary
kandi X-RAY | linked-list-good-taste Summary
In a 2016 TED interview (14:10) Linus Torvalds speaks about what he considers good taste in coding. As an example, he presents two implementations of item removal in singly linked lists (reproduced below). In order to remove the first item from a list, one of the implementations requires a special case, the other one does not. Linus, obviously, prefers the latter. [...] I don't want you to understand why it doesn't have the if statement. But I want you to understand that sometimes you can see a problem in a different way and rewrite it so that a special case goes away and becomes the normal case, and that's good code. [...] -- L. Torvalds. The code snippets he presents are C-style pseudocode and are simple enough to follow. However, as Linus mentions in the comment, the snippets lack a conceptual explanation and it is not immediately evident how the more elegant solution actually works. The next two sections look at the technical approach in detail and demonstrate how and why the indirect addressing approach is so neat. The last section extends the solution from item deletion to insertion.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of linked-list-good-taste
linked-list-good-taste Key Features
linked-list-good-taste Examples and Code Snippets
Community Discussions
Trending Discussions on linked-list-good-taste
QUESTION
I'm trying to implement the linked list by indirect pointer, which introduced on the TED Talk.
I refer to felipec's implementation on github and made an version by raw pointer, this is the github link.
This is part of the full code, _find
is a function that can find the indirect pointer of target node, which means *indirect == target node
:
ANSWER
Answered 2021-Dec-26 at 19:47How could I get the address of the target object in smart pointer?
I think you mean "the address of the pointer in the smart pointer", because the address of the target object is just what you get with .get()
. std::unique_ptr
does not offer any interface to access it's pointer member directly, so it is impossible.
Did I use the smart pointer in correct way? Should I change shared_ptr to unique_ptr, or just not use smart pointer for it?
Using std::unique_ptr
is the correct thing to do. You don't need shared_ptr
because there are never two owners of any given node.
In your first code indirect
is a pointer to a raw pointer in the Node
/List
structure. Since you changed these to smart pointers, indirect
should also become a pointer to a smart pointer:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install linked-list-good-taste
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page