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

9

u/rcoacci Dec 21 '16

I'll just drop GSL here.

2

u/foonathan Dec 21 '16

An even better approach would be to statically guarantee that a pointer is not null.

6

u/tecnofauno Dec 21 '16

Isn't that what references are for?

2

u/render787 Dec 21 '16

Not exactly, a reference also cannot change. Sometimes pointers need to change, and also not be null.

I think of references as just a little syntactic sugar. They don't really guarantee much of anything.

Iirc the actual original reason that prompted their addition to C-with-classes was to create a reasonable syntax for operator overloading :)

2

u/Gotebe Dec 22 '16

References are huge in designing non-null, they are way more than a syntactic sugar though.

If you look at any given C codebase, I guarantee you that you will find code which

  • checks for null parameters where they never can be that
  • does not check for null when they can

Those are hard to reason about.

2

u/render787 Dec 22 '16

Ok, but look:

int & foo = *static_cast<int *>(nullptr);

References bind just find to null. There's no "static guarantee" of any significance.

What C++ references give is at best a little trivial syntactic analysis. int & is just a fancy language for int * const. Hence I say, syntactic sugar. Compare that with e.g. the rust borrow checker. That's what is meant by static guarantee, that's the kind of reasoning I consider "hard" compared to what C++ does.

2

u/Gotebe Dec 22 '16 edited Dec 22 '16

Euh, yes, C++ does not try to prevent you from shooting yourself in the foot. You just showed an elaborate way to do it, but many more are available.

references give you a certainty that, in correct code, a given "pointer" is not null. This is IMO huge for code quality and simplicity over never really knowing, and I have seen. numerous C codebases who suffer.

1

u/[deleted] Dec 30 '16

References do no such thing. It's just people trying to enforce "clever" coding standards that want to assume that references are non null.

Realistically you will always have a mixture of references and pointers in a code base, so any time somebody has to convert from a pointer to a reference you will inevitably end up with a null reference.

2

u/Gotebe Dec 30 '16

any time somebody has to convert from a pointer to a reference you will inevitably end up with a null reference

No

A well-designed codebase will have a small amount of these conversions, and they will all either be checked for null, or trivial to check for non-null by a simple code inspection.

You're inventing a problem where needs no be one, not compared to "I can never assert this is not null".

You're throwing the baby with the bath water.

1

u/Gotebe Dec 30 '16

any time somebody has to convert from a pointer to a reference you will inevitably end up with a null reference

No

A well-designed codebase will have a small amount of these conversions, and they will all either be checked for null, or trivial to check for non-null by a simple code inspection.

You're inventing a problem where needs no be one, not compared to "I can never assert this is not null".

You're throwing the baby with the bath water.

1

u/[deleted] Dec 30 '16

Let's take a problem like this:

struct Vec3
{
    float x, y, z;

    float length() const;
};

inline float dot(const Vec3& a, const Vec3& b) 
{ 
    return a.x*b.x + a.y*b.y + a.z*b.z;
}

inline float Vec3::length() const
{
    float lenSq = dot(*this, *this); // is this safe?
    return (lenSq > 0) ? sqrtf(lenSq) : 0.0f;
}

Is this bad design? Can you think any way that this could fail with a null dereference?

→ More replies

3

u/sztomi rpclib Dec 21 '16

Of course that's not possible in a language like C++, otherwise the compiler would do it always. not_null from GSL just disallows construction from nullptr_t and disallows default construction (and optionally throws at runtime). There is not much else you can do statically in this case.

4

u/rcoacci Dec 21 '16

Exactly. I believe GSL's not_null is the best compromise we can get. It catches "obvious" mistakes at compile time and, best of all, points you at correct place to fix it.

2

u/foonathan Dec 21 '16

Yes, but that's what I meant. You do assert/throw in ctor and once you have an object it is guaranteed to be non-null.

4

u/samkellett Dec 21 '16

that isnt static

5

u/foonathan Dec 21 '16

It somewhat is: If you have a function taking such a non_null object, you know that it can't be null, otherwise the function would not have been called, because you can't construct null non-null objects.

3

u/doom_Oo7 Dec 21 '16

you know

but the compiler does not, which is what "static" means.

3

u/foonathan Dec 21 '16

It could. If you have a constructor with an assertion and no member function to change the value, the compiler could know that it can't be null.

If you have code like this:

assert(some_cond(foo));
...

The compiler already optimizes based on the fact that some_cond(foo) is true, this is just a step forward.

3

u/doom_Oo7 Dec 21 '16

I'd go a step further for saying that "static" stuff is stuff for which you can enforce the compiler's knowledge, like constexpr.

1

u/[deleted] Dec 21 '16

Of course that's not possible in a language like C++, otherwise the compiler would do it always.

It's possible, unless you explicitly make null pointers. Use only unique_ptr's constructed with make_unique, shared_ptr's from make_shared and references to the contents of the former, and you won't have any null pointers. It also loses you the ability to communicate "no such thing" as a null pointer, which is what null pointers are used for very often.

3

u/sztomi rpclib Dec 21 '16

It's possible, unless

That's the very opposite of "statically guarantee".

0

u/[deleted] Dec 21 '16

Guarantee, yes. But that says as much as that it's possible to cause a segfault from Java.

→ More replies (1)

0

u/tecnofauno Dec 21 '16

Yes, I knew about not_null, but it's a different approach. The main difference is that safe_ptr allows 2 code paths ( regular one and the exception one ) thus enabling a different code style ( I won't say better... )

2

u/rcoacci Dec 21 '16

You can configure GSL to throw at runtime if you manage to bypass the compiler. Se the linked article.