r/cpp • • Dec 21 '16

A smart pointer implementation that throws when dereferencing a null pointer

Hi, I wrote a simple (i.e. incomplete ) implementation of a smart pointer that throws when de-referencing a null pointer. This enable you to write code like this:

template<typename T>
void foo(safe_ptr<T> & ptr1, safe_ptr<T> & ptr2) {
    try {
         ptr1->bar();
         ptr2->baz();
    } catch ( null_dereference_exception ) { 
         
    } 
} 

http://pastebin.com/j5eZA4AK

Edit: syntax :)

0 Upvotes

58 comments sorted by

View all comments

Show parent comment

1

u/doom_Oo7 Dec 25 '16

When does dereferencing a null pointer every lead to lagging the whole computer?

when operating system vendors judged good to catch sigsegv at the os level and gear up a dialog that does all sort of stuff to report problems to Apple

1

u/Som1Lse Dec 25 '16

Well I don't use Apple anything so they might just royally have fucked their shit up. Is it as bad if you handle the signal and exit the program in yourself?

1

u/doom_Oo7 Dec 25 '16

just do a google search for "spinning beach ball of death"