In a naive reference-counting smart pointer implementation, the reference-counter could overflow. How is this overflow avoided or handled in C++ standard library implementations?
Snippets from stdlibc++ headers:
typedef int _Atomic_word;
class _Sp_counted_base
/*snip*/
_Atomic_word _M_use_count;
/*snip*/
_M_weak_add_ref()
{ __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); }
/*snip*/
__atomic_add_dispatch(/*snip*/)
{
/*snip*/
__atomic_add_single(/*snip*/);
/*snip*/
}
__atomic_add_single(/*snip*/)
{ *__mem += __val; }
Conclusion: This particular implementation "handles" reference-counter overflow by ignoring the possibility.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With