Hi Eric im not sure which part @6:22 that was working but if you are refering to destroying node that contains data and returning their pointer .. then it can happen and work .. its just called dangling pointer .. the os is responsible to allocate you the heap memory when u destroy it it doesnt change the actual memory.. its just free to use again by you or other softwares ..
@eom-dev3 жыл бұрын
Interesting, thanks for letting me know!
@EverythingExceptThat4 жыл бұрын
Great video! I’d be interested to hear more about where the performance penalty comes from here, is it just because of more dynamic allocations?
@eom-dev4 жыл бұрын
That is exactly right! Allocating items to the stack requires a single CPU command, whereas allocating to the heap requires several. I have implemented dynamic allocation twice here: first for the nodes themselves, and then again for the data contained in the nodes. If I was using a linked list that could only contain integers, I would be able to store the object's data within the same action as the one that allocates space for the node. Essentially, calling malloc once instead of twice. What is interesting is that, despite this performance cost, the program still is faster than the C++ version! I am going to make a video on this soon, but using a class template in C++ is actually slower than calling malloc twice. The difference is minute but measurable, so here again we are faced with a tradeoff between convenience and performance!