Re: shared_ptr::operator-> does not fail for nullptr

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 11 Jun 2014, Christoph Mathys wrote:

Hi, I hope I found the right list.

I stumbled across this (at least for me) surprising behaviour of
std::shared_ptr. I can call operator-> or operator* on an empty
shared_ptr, but my program will not crash in all cases.

I'll try to illustrate my problem with examples:
std::shared_ptr<int> pi;
int& i = *pi; // no crash ?!?

references are syntactic sugar around pointers, to the compiler this is roughly:
int* i_ptr = pi.get();

This is a bug in my program, but the program only crashes when it
tries to access i, which might be quite far away from where I made the
mistake.


std::shared_ptr<int> pi2;
int i2 = *pi2; // crash segfault

Here, my program will crash right when dereferencing the pointer.

Now, is this the expected behaviour of shared_ptr? Do I need to check
myself that my smart_ptr is non-nil similar to "manual" bounds
checking when using operator[] on std::vector?

I tested this using gcc 4.6.4 and 4.8.1 on Linux with c++11 enabled.
Just for information: The shared_ptr of boost 1.54 crashes with a
failed assertion.

If you compile with -D_GLIBCXX_DEBUG there will be an assertion.

--
Marc Glisse




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux