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 comments

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 (0)