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

Show parent comments

0

u/[deleted] Apr 05 '22

[deleted]

5

u/ShelZuuz Apr 06 '22 edited Apr 06 '22

And that's assuming your array even has space! What if your array is full and you want to add more stuff? Well, you can't just say "I want a bigger block", because this array is already built and there might be stuff next to it. So what happens is you make a new array somewhere, double the size, and then move everyone from your old array into your new array. As you can imagine, it's not the most efficient process.

So just want to point out, this is theoretical. On paper it's faster inserting into the middle of a linked list if you assume all memory accesses are the same.

However, it's not because of how memory prefetching works. If you start accessing elements in an array, the prefetcher will realize you're doing sequential access and fetch additional elements even before you ask for it. It can't do this with a linked list as there are pointers stored in there and the prefetcher has no idea how to traverse them.

In practice it means that duplicating an entire array and adding an element to the middle is faster than traversing into half of a linked list and adding an element there (by a lot).

Even if you only add to the end of a linked list, you almost have to have a write-only linked list for it to be worthwhile.

Except... if you have a write-only linked list you create another HUGE problem for yourself... Every insert into a linked list is a memory allocation. Not so with an array. Let's say you have an array that you double in size each time it's full and then duplicate everything in there. Then you insert 1 million items into it. With an array, that's 20 allocations and copies. With a linked list, that's 1 million allocations. No copies, but the extra allocations FAR outweigh the 20 copies.

The only thing where a linked list outperforms an array, is if you have to randomly insert elements into both ends, but in that case, a deque will outperform a linked list again (and a deque is internally implemented as arrays).

1

u/SwishArmyKnife Apr 05 '22

The movie row analogy really makes it make sense. So, if the memory doesn’t necessarily need to be continuous, is there anything that needs to be continuous in relation to node links for the computer to know where the next data point is?

0

u/[deleted] Apr 05 '22

[deleted]

2

u/SwishArmyKnife Apr 05 '22

That makes so much more sense than…whatever I thought was happening. I’m gonna take another look at it and see if I understand it this time. Thanks so much!

1

u/InfiniteBuddy1269 Apr 06 '22

Excellent explanation!