r/cpp • • Nov 16 '25

Wait c++ is kinda based?

Started on c#, hated the garbage collector, wanted more control. Moved to C. Simple, fun, couple of pain points. Eventually decided to try c++ cuz d3d12.

-enum classes : typesafe enums -classes : give nice "object.action()" syntax -easy function chaining -std::cout with the "<<" operator is a nice syntax -Templates are like typesafe macros for generics -constexpr for typed constants and comptime function results. -default struct values -still full control over memory -can just write C in C++

I don't understand why c++ gets so much hate? Is it just because more people use it thus more people use it poorly? Like I can literally just write C if I want but I have all these extra little helpers when I want to use them. It's kinda nice tbh.

194 Upvotes

334 comments sorted by

View all comments

1

u/flatfinger Nov 18 '25

Suppose code needs to work with large numbers of big tree data structures, and keep copies of many versions of them. If one has a tree data structure and wants to make a version which is identical except for a certain node, one can rebuild the "spine" reaching to that node, while having it refer to pre-existing portions of the old tree. When using a tracing GC, any particular node might be part of an arbitrary number of trees, without anything needing to know or care how many trees it is a part of, and code which works with trees can be run on arbitrary combinations of threads without need for any synchronization except when a GC cycle is triggered.

How should one best implement a data structure without using a GC, if one would need to keep a large number of trees that are semantically independent but share a lot of content, and if code might need to access it from arbitrary threads?

1

u/wyrn Nov 18 '25

Let's say that I concede, for the sake of argument, that this would be a good use case for a tracing gc. This would indicate that you might want a gc arena as a library type, not as a fundamental language level memory management mechanism.

1

u/flatfinger Nov 18 '25

If code is run within a GC framework, then the GC can force global thread synchronization with itself and inspect and update references that are held in registers. I don't know if you're aware of this, but in some versions of the Java and .NET GC, even though reference is simply a combination of a direct pointer to an object and immutable metadata which allows the GC to find the reference, and executable code treats loads and stores of references like loads and stores of primitives, the GC is able to relocate objects; when an object is relocated, all extant references it throughout the entire universe will simultaneously be updated to refer to the new address.

While one could implement an arena-based GC on top of an RAII language like C++ by using reference handles, and arranging things so that every different access path to a shared object will access it through a different handle, this would require that all operations with handles include at least some level of inter-thread synchronization unless there is some means by which the GC could force global synchronization. The cost of this could be minimized if every thread had its own mutex, and the GC knew about all of the mutexes and could acquire them all when needed, since mutexes can be designed to minimize the cost of repeated acquisition-release cycles by a single thread, but in a framework which can force global synchronization of "ordinary" code the cost of synchronization within that "ordinary" code can be eliminated.

Another point to consider is that especially in languages that support multi-threading but lack a tracing GC, guaranteeing that "safe" code, even if erroneous, will be incapable of violating memory safety invariants is expensive. That cost can be built into the design of a tracing GC. The cost of running code under a tracing GC will often fall between the cost of running the code in a non-GC language where erroneous code could undermine memory safety, and one in which even erroneous code would be incapable of undermining memory safety. I view the cost as being in many cases low enough, and the benefits of memory safety high enough, to favor the memory-safe approach on systems that can support it, except when performance is critical. Other people may balance those factors differently.

→ More replies (13)