How ?

What features to simplify GC are in C ++ 11?

Fundamentals of memory management in C ++ (redefining new / delete for classes for which we want strange):

Garbage collection:

A garbage collector for C and C ++ - Hans Boehm

Closed due to the fact that the issue is too common for participants Vlad from Moscow , Harry , Alex , Kromster , user194374 18 Dec '16 at 17:07 .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 7
    Purely not where they clean, but where they do not litter. - Vladimir Gamalyan
  • A garbage collector - as I understand it - a library. This is with the # collector integrated, and with the ++ there is no staff. And with ++ 11 regular also is not present. Here are the functions your junk library has. - nick_n_a
  • one
    @nick_n_a, what about shared_ptr and company? - Qwertiy
  • There are very few of them and they fit into one page . So far, GC for C ++ is not in the standard. - ixSci
  • 2
    @Dmitry Ponyatov There are no tools in C ++ to simplify the creation of a GC. The problem is that you need to change the language itself, since it will be necessary to change the ideology of working with resources in the client code. So if you need GC that way, then deal with C ++ / CLI. - Vlad from Moscow

1 answer 1

In C ++, there are no and cannot be garbage collection tools. Smart pointers can be used instead. The most versatile of them is shared_ptr . In the constructor of a smart pointer, you must pass a pointer to an already allocated memory, and nowhere else can you use this pointer. When a shared_ptr object is copied, when it is passed as a function parameter, the internal counter of objects that contain the same pointer increases. As soon as the last shared_ptr object is destroyed (goes out of scope), the memory referenced by the internal shared_ptr pointer is automatically freed. Here is an example of use.

 void foo() { //Создаём умный указатель, передав ему обычный указатель на массив из 10 целых чисел std::shared_ptr<int> x(new int(10)); //Вызываем функцию, которая использует этот указатель. На стеке создаётся копия объекта x, которая указывает на ту же память bar(x); //Заканчивается область видимости, удаляется x, освобождается память по данному указателю } void bar(std::shared_ptr<int> y) { //Функция работает со своей копией объекта y[5] = 10; //Область видимости закочилась, объект y удаляется, но так как есть ещё один такой же умный указатель, память не освобождается } 
  • 3
    It should be added that shared_ptr has a drawback: ring links require manual loop breaks (which forces you to care about the exact moment of the destruction of the data structure). - VladD
  • one
    @VladD, weak_ptr - yrHeTaTeJlb
  • one
    @yrHeTaTeJlb weak_ptr able to find and break ring links? - Cerbo
  • @yrHeTaTeJlb: Well, firstly, this is not in the answer, and secondly, the task is added to correctly allocate pointer types so that the desired data structure does not accidentally end up without strong references. This may well be non-trivial for non-trivial relations between objects (that is, where there is no self-evident tree). - VladD
  • one
    @Cerbo, find - no, break - yes - yrHeTaTeJlb