4 ms·
I've spent roughly 20 years going back and Forth between C and C++, trying to decide whether to add the missing pieces my way to C or strip away what I don't ne
by codr7 7y ago
I've spent roughly 20 years going back and Forth between C and C++, trying to decide whether to add the missing pieces my way to C or strip away what I don't need from C++. I'm currently in a C phase after having spent quite some time writing interpreters in C++.
The most common issue I see in C codebases is excessive memory allocation. In most other languages, allocating a chunk per instance is the only way, though the implementation likely does some kind of pooling/slab allocation behind the scenes. In C you have the freedom to arrange your allocations any way you feel like, and treat any segment of N bytes as a value of size N. As an example, most collections (vectors, sets, maps etc); tend to only work with pointers; which is a huge missed opportunity.
My two favorite kinds of collections in C are embedded linked lists [0] and deques. One reason is they both provide stable pointers (which is the main disadvantage of vectors), the other that they minimize the number of memory allocations.
I recently implemented a pool allocated deque library [1] in C that uses embedded linked lists to keep track of memory blocks and provides value semantics, which I hope will make my interpreters easier to implement as well as increase performance. Time will tell.
[0] https://github.com/codr7/libceque/blob/master/source/libceque/list.h https://github.com/codr7/libceque/blob/master/source/libcequ...
[1] https://github.com/codr7/libceque https://github.com/codr7/libceque