r/learnprogramming • • Apr 05 '22

Name one programming/comp sci concept you never understood and if you understand it, try to explain it to them

Name a programming concept such as virtual functions, pointers, certain algorithms, etc that you always had a hard time understanding and why.

Someone else who reads yours and does understand it, try to explain it to them to help them learn

1.6k Upvotes

738 comments sorted by

View all comments

14

u/[deleted] Apr 05 '22

linked lists bruh i cannot get it rn

-2

u/ShelZuuz Apr 06 '22

You will one day.

And then soon after you do, you'll learn never to use them in practice... there are outdated with modern day memory controller architecture.

1

u/warlaan Apr 06 '22

Linked lists in the strictest sense yes, but the concept is not. For example if you are working on a potentially huge data set you would want an array for performance reasons, but with an array you would need to know in advance how large it must be.

If you have no way of knowing the size in advance you have a problem. You could use a vector (i.e. an array that is resized when it becomes too small), but if the data set is so large that you can't keep all of it in memory at the same time you still have a problem.

One easy solution is to use something in between linked lists and arrays by simply creating "liked arrays". You start with an array of a predefined size, and when it is filled up you append a link to a second array of the same size. That way you have (almost) the performance of arrays because you rarely need to make those "linked list jumps", but you also don't have the performance overhead of having to copy huge arrays when resizing that container, because now you can simple remove whole chunks from the linked list part.

1

u/ShelZuuz Apr 06 '22

Unless you have esoterically large elements, a linked list will take up more than twice the amount of space as an array in memory.

Pointer extra per node as well as alignment and minimum allocation requirements from the Heap Manager.

If you can store a linked list you can store, grow and shrink a vector in the same amount of memory, and it will outperform the linked list in virtually every other way.

1

u/warlaan Apr 06 '22

I am not sure if you even read what I wrote. I am specifically talking about linking large arrays.

1

u/ShelZuuz Apr 06 '22

I am not sure if you even read what I wrote. I am specifically talking about linking large arrays.

I did. That's called a deque (in C++ at least), but in the sole exception of a queue that needs double-ended insertion, the benefits of a deque does not generally outweigh the drawbacks. But either way, in that one scenario a deque definitely has a place - I'll grant you that.

However, you started by saying: "If you have no way of knowing the size in advance you have a problem". That's not actually a problem when just comparing a list to a vector.

Adding an unknown number items to the end of a list vs. a vector, inserting into the vector is going to keep doubling the size of the vector when it gets full and then copy over all of the elements to the new allocation. That is still order of magnitudes faster than inserting into the end of a list. And once you're done inserting, you can copy the vector to another vector that's the exact right size you want. With all that, the whole process will still be 10 times faster than inserting into the end of a list, and at no time in between will the vector (or two times the size of the vector if you include the temporary to make a copy), take up more space than the list.

The only consistent advantage a list has over a vector is double-ended insert/delete. (But the deque is better at it again).

However, when you look at code from juniors in a codebase, you'll see std::list all over the place rather than std::vector because they just learned the basics of it in Computer Science class with O-theory behind it and they think that inserting into a list actually is faster or takes less space than inserting into a vector. They're not doing it because they are writing queues that need actual double ended insertion.

I mean lists has to be taught because it forms the basis of binary trees, but I wish after that people will forget about them and stop attacking my codebase with std::list all over in critical perf paths that I have to go fix years later.

Speaking of binary trees... they're also only better if you have a binary tree that's very often being written to after initial construction. If you have a binary tree that's predominantly read from, you're again better off inserting into a vector, doing a sort afterwards and searching it with standard binary search. My ultimate pet peeve is when people get already ordered data from SQL inside a vector, and then go store it into a binary tree for cached access. Like ... duuude.

→ More replies (2)