3 ms·
> On the other hand, C++ has more flexibility. Since C++ allows you to specify your allocation scheme, you can guarantee a piece of code will never trigger an a
by exDM69 13y ago
> On the other hand, C++ has more flexibility. Since C++ allows you to specify your allocation scheme, you can guarantee a piece of code will never trigger an allocation, allocate in a special way to ensure cache coherence, etc.
This is true but the way C++ standard containers work with allocators is rather clumsy. So clumsy that often when custom allocation logic is required, standard containers are not used at all and custom containers are built instead. For example, EA has used a custom STL-esque container library to avoid memory allocation pressure in console and PC games.
> For example, you could have written equivalent logic that allocates odd_numbers on the stack or from a special memory pool.
But this example does not require any allocation space at all. But there's no way of doing this with standard C++ algorithms without allocating additional space.
- bronxbomber92 13y agoLuckily functions from <algorithm> don't only work with STL containers; they work with any class that adheres to the STL containers interfaces.
- humanrebar 13y ago> But this example does not require any allocation space at all. That's not possible if you're comparing algorithms apples-to-apples. If you have an intermediate collection, it's in memory somewhere, even if the language does the allocation for you transparently.
- exDM69 13y ago> If you have an intermediate collection, it's in memory somewhere, even if the language does the allocation for you transparently. This toy example algorithm, and many practical algorithms, do not need an intermediate collection to be stored in memory. Using some kind of laziness, you only need exactly one temporary element, not the whole collection. You can make this happen not only in Haskell but also using Python generators, Clojure lazy sequences or even in C++ using Folly. The lack of means of composition, the inability to pipe the output of one algorithm as the input of another, is in my opinion the greatest shortcoming of C++ <algorithm>.