Using Boehm's Garbage Collector with C Standard Library
In this context, a developer expresses their desire to utilize Boehm's garbage collector in a multi-threaded C application. They intend to exploit the C standard library's algorithms and collections, such as std::vector and std::map, but are concerned about potential conflicts.
Redefining operator new
The questioner contemplates redefining operator new with Boehm's implementation. However, they also consider using the collection templates with an explicit allocator argument set to gc_allocator.
Role of the Allocator Argument
The second template argument in std::vector defines the allocator used to manage the vector's internal data. By default, it allocates memory using the system's malloc, but it can be customized to use alternative allocators like gc_allocator.
Handling std::string
The developer expresses concerns about GC-allocating std::string instances. They propose creating a custom string using basic_string with gc_allocator. Alternatively, they inquire about GC-allocating the internal char arrays.
Advice on GC Compatibility
The questioner seeks recommendations on whether to use Boehm GC with an application compiled by g .
Possible Solution
The developer shares their own solution code, which employs gc_allocator and new(GC) to ensure that all heap allocations are GC-managed.
Addendum
The developer discusses the proposed n2670 specification, which aims to provide garbage collection support in C . However, they note that this feature is not currently implemented in major compilers like GCC or Clang.
The above is the detailed content of Here are a few title options, taking into account the question-and-answer nature of the article: **. For more information, please follow other related articles on the PHP Chinese website!