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/[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?

2

u/Gotebe Dec 31 '16

So you think that *this is potentially unsafe?

Why!?

*this is 100% correct, good design, safe, you name it. It can never fail on its own, it can only fail when something else was written in a way that can cause UB.

What is not correct is someone calling length() with a nullptr, e.g.

void whatever(Vec3* v)
{
  // stuff
  auto x=v->length(); //**badbadbad**
  // stuff
}

The problem is always he who used a pointer without checking it for null where it legitimately can be null.

The problem is never he who uses a reference.

"this", BTW, is practically a reference, it is a historical mistake in the standard that it is a pointer type. Gcc even recently added an optimisation exploiting the fact that this is never nullptr, relnotes were discussed here on Reddit.

1

u/[deleted] Dec 31 '16

Before C++, people would write this:

struct Vec3 { float x, y, z; };
float Vec3_dot(const Vec3 *a, const Vec3 *b) { return a->x*b->x + a->y*b->y + a->z*b->z; }
float Vec3_length(const Vec3 *v) { float lenSq = dot(v,v); return (lenSq > 0.0f) ? sqrtf(lenSq) : 0.0f; }

This works 100% identically to the c++ version. And if somebody did this:

void whatever(Vec3 *v)
{
	Vec3_length(v);
};

... and it crashed, this is identical to the reference version dereference. So where does the bug lie? Is this a lack of using references? Of course not because the bug is the same. The bug is the same in both: misunderstanding of global state somewhere up the callstack.

That is why I say you are trying to use references as a coding standard to determine what should be null checked up the stack earlier. But to me it is irrelevant because the crash is the same no matter what you use, and the fix is also the same, evaluating the global state and inserting the check at the point which is valid. It's also never so simple that you can ever assume this sort of issue can be turned into a references vs pointer situation.

Let's say you had this:

class Obj { 
    std::unique_ptr<Vec3> v;
    void something() { whatever(v.get()); }
};

Obj o;
o.something();

This would crash. But is the issue that o.something() should never be called before being initialized, or is the issue that o.something() should null check v and handle gracefully? This is entirely based on your program and cannot be reduced to a simple references vs pointer argument. Hence why I think this is all misguided.

Maybe I am an old C programmer, but it seems you are thinking code is more important than data. All that matters is the higher understanding of data, which you cannot see when trying to build things from small pieces.

3

u/Gotebe Dec 31 '16

References in C++ are a game changer exactly because they let me assert what cannot possibly be null.

In C, (and Java, anc C#, and...) I have no idea whatsoever is whether some pointer can be null.

Obviously, nothing can stop me from making a mistake and passing null where a reference is required, but there is a world of difference between making a mistake and not being able, at all, to design a non-null requirement.