Re:Collections-C, generic data structures for C
by gibsjose on Sunday March 13, 2016 @03:43PM
(#11279040)
If you are using an RTOS (which many timing-critical applications run on), you should be worried about memory allocations happening under the hood.
If you don't have full control over when memory is being allocated/reallocated, your system is now non-deterministic.
With pure C, you can know exactly when those few extra instructions for resizing your dynamic array are going to happen.
Re:Collections-C, generic data structures for C
by catnaroek on Sunday March 13, 2016 @05:34PM
(#11279452)
Same in C++. There's `std::vector::reserve`, which grows the vector's underlying physical buffer, without logically adding any new elements. If you `reserve` enough capacity before inserting anything, it's even a `O(1)` operation.
Re:Collections-C, generic data structures for C
by gibsjose on Sunday March 13, 2016 @08:12PM
(#11280133)
Vector and possibly string have that. The vast majority of STL structures do not.
Re:Collections-C, generic data structures for C
by catnaroek on Sunday March 13, 2016 @08:23PM
(#11280168)
All containers from the C++ standard library (please don't call it STL, that's Stepanov's original library) can be parameterized by an allocator. You can use whatever allocation policy you like best. However, most people use the default allocator because it's good enough.
In any case, while C++ has lots of defects, “loss of control relative to what C gives you” isn't one of them.
Re:Collections-C, generic data structures for C
by optforfon on Monday March 14, 2016 @03:38AM
(#11281520)
(a bit of topic - but I'm humbly trying to learn more...)
I've got zero experience writing allocators. Is there some common ones you use provided somewhere? Do you write your own? (in which case can you point me to where to learn to do that properly)
Re:Collections-C, generic data structures for C
by catnaroek on Monday March 14, 2016 @09:23AM
(#11282701)
I don't write allocators myself, but Boost has a pool allocator library [http://www.boost.org/doc/libs/1_60_0/libs/pool/doc/html/inde...], which conforms to the Allocator concept defined in the standard library [http://en.cppreference.com/w/cpp/concept/Allocator].
If you don't want to use this style of programming, for whatever reason, check out sys/queue.h. It's already on your system, if you're using some kind of Unix.