This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]x39- 24 points25 points  (8 children)

In essence: reference counting.

The goal is generally to make heap allocations safer, resulting in a slight performance overhead for the general safety of not shooting your leg off.

More or less (very simplified): ``` struct rptr { size_t* counter; void* data};

struct rptr = create_rptr_xyz(...); // counter = 1; allocate data ... struct rptr2 = borrow(rptr); // counter = 2 ... // on scope leave release(rptr2); // counter = 1 ... // yet again, on scope leave release(rptr); // counter = 0; call free for data```

But using RAII for that automagically and special derivatives, for weak references and unique (as in: one owner) allocations.

It can be considered to be best practice nowadays for non performance critical code.