6 ms·
tl;dr: 1) use -Wall, -Werror, -W4. I suggest -pedantic as well; surely you were already using the first two? 2) designed initializers are nice Indeed, they
by sagarm 7y ago
tl;dr:
1) use -Wall, -Werror, -W4.
I suggest -pedantic as well; surely you were already using the first two?
2) designed initializers are nice
Indeed, they are.
3) you don't need RAII because it's only for memory
False. C++ is my main language, and we use RAII in many ways: callback invocation, taking locks, pinning cache items, timing segments of code, etc etc.
4) small allocations are bad
Small allocations are indeed highly inefficient, but this can be addressed using arena allocation. This works because it's actually the deallocations and associated defragmentation that are expensive.
5) ...so the solution for memory management is not to have structs that contain pointers (?)...
You should use the simplest data model possible, but only the simplest data models don't require having pointers in structs. For example, any recursive structure would have to have a pointer.
6) and all your objects should be allocated from object specific free lists
Having an object pool doesn't address memory leaks. You would still have to return the object to the pool at some point.
- ncmncm 7y agoA good summary. Notably, none of these constitutes any sort of argument against just compiling with the C++ compiler, and relaxing into its objectively superior for-loops and std::array types -- to start with. Once there, of course, the objectively more powerful, safer, faster (than can be coded in C) C++ libraries come into reach, and you are off to the races. If you have been deluded that C++ is all about virtual functions and heap allocations, you are in for a happy surprise. (My programs run for days or weeks using neither.) The most distinctive development in C++ in the past decade is that it is overwhelmingly more fun to code in, than before, and than C. Different people trace the fun to different places, but for me it centers in generic lambdas. I like to use them to invert control structures and cut down repetitive code.
- jplayer01 7y agoIs arena allocation something that you implement anew for every project or is there a suggested library or...? New to C++. Also, how does this interact with smart pointers?