I want to develop a multi-threaded C++ application (where eventually most of the C++ code would become generated by the application itself, which could be viewed as a high-level domain specific language) on Linux/AMD64/Debian with GCC 4.6 (and probably latest C++11 standard).
I really want to use Boehm's conservative garbage collector for all my heap allocations, because I want to allocate with new(GC)
and never bother about delete
. I am assuming that Boehm's GC is working well enough.
The main motivation for using C++ (instead of C) is all the algorithms and collections std::map
... std::vector
provided by the C++ standard library.
Boehm's GC provide a gc_allocator<T>
template (in its file gc/gc_allocator.h).
Should I redefine operator ::new
as Boehm's one?
Or should I use all the collection templates with an explicit allocator template argument set to some gc_allocator
? I don't understand exactly the role of the second template argument (the allocator) to std::vector? Is it used to allocate the vector internal data, or to allocate each individual element?
And what about std::string
-s? How to make their data GC-allocated? Should I have my own string, using basic_string
template with gc_allocator
? Is there some way to get the internal array-s of char allocated with GC_malloc_atomic
not GC_malloc
?
Or do you advise not using Boehm GC with an application compiled by g++ ?
Regards.
To answer partly my own question, the following code
when compiled with
g++ -O3 -Wall -c myvec.cc
produces an object file withSo there is no plain malloc or
::operator new
in the generated code.So by using
gc_allocator
andnew(GC)
I apparently can be sure that plain::opertor new
ormalloc
is not used without my knowledge, and I don't need to redefine::operator new
addenda (january 2017)
For future reference (thanks to Sergey Zubkov for mentioning it on Quora in a comment), see also n2670 and
<memory>
and garbage collection support (like std::declare_reachable, std::declare_no_pointers, std::pointer_safety etc...). However, that has not been implemented (except in the trivial but acceptable way of making it a no-op) in current GCC or Clang at least.