3 ms·
I think that when the code base is small it can be really fitting. Ones I find my favorite do almost no memory allocations. Or allocations are done only for the
by hawski 4y ago
I think that when the code base is small it can be really fitting. Ones I find my favorite do almost no memory allocations. Or allocations are done only for the workset. I do not like that nowadays it seems customary to heap allocate everything and tie object's lifetime to where it keeps its state. Modern C++ with unique_ptrs sprinkled everywhere is like that.
- Thorrez 4y ago>and tie object's lifetime to where it keeps its state What's the objection there? I want the state to be freed when the object's lifetime ends. For something only needed during a function, a local value variable makes sense. For something that needs to last longer, either give it a move operator, or put it on the heap and use unique_ptr. If your point is that people sometimes use unique_ptr<std::string> instead of std::string or std::optional<std::string>, then yeah I agree, that's generally not good.
- eska 4y agoThat’s not his point, but you demonstrated it quite well. C++ use leads to developers that conflate memory allocation and initialization. For contrast, objective C also differentiates between the two.
- hedora 4y agoAllocation and initialization should be atomic. Otherwise, the rest of the program has to deal with uninitialized objects and no invariants hold.
- qalmakka 4y agoIn well written modern C++ there are rarely memory allocations outside of STL containers. I have worked on big embedded projects that only use unique pointers in very few locations, you can do a lot of stuff without using the heap if you really want.
- FpUser 4y agoCan confirm. My latest backend C++ server of decent size has just a single explicit allocation happening upon startup. In my C firmware projects I do not allocate memory at all. Everything is predetermined.
- hedora 4y agoI guess I mostly wrote postmodern C++ then. The stl containers aren't lock free, and std:strings allocation behaviors are too confusing for me. Having said that, "delete" hardly showed up at all.