Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You would need to do the same in C++, since the underlying dynamic array is being resized throughout the algorithm, i.e. pointers to its elements are being invalidated. Except C++ won’t warn you about it. You’ll just corrupt memory.

And in garbage collected languages you’d do the same to avoid the cost of GC.



In C++ you can trivially opt out of using a dynamic array.

There are plenty of reasons not to use C++, including the lack of memory safety, but if you've already decided to ditch that, C++ does let you pick and choose which parts you want to use.


> does let you pick and choose which parts you want to use

Isn't that the case for most programming languages (ie, you can use the implementation you want/need), though?


Not to the extent of C++. E.g. you can't opt out of dynamic dispatch overhead in Ruby, because everything is method passing, but "virtual" in C++ is a choice.

The commitment to ensure that features do not add cost(memory, runtime) if you don't use them is something fairly uncommon in other languages, and many languages have features you can't meaningfully avoid.


You don't have to use an array based on realloc. You can use a vector based on linked list of memory pages, or linked list if you need deletion (or gap buffer or any other data structure depends on your needs).


And at that point you’re slower.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: