Remix.run Logo
maccard 5 hours ago

Indeed.

   bool is_even(int* valPtr) {
      assert(valPtr != nullptr);
      return *valPtr % 2;
    }
Does not do what you think it does with nullptr. A major game engine [0] has a toggle to enable asserts in shipping builds, mostly for this reason

[0] https://dev.epicgames.com/documentation/en-us/unreal-engine/...

mhh__ 4 hours ago | parent | next [-]

This is a very "Dr Dr it hurts when I do this" "Don't do that" one it must be said.

secondcoming 2 hours ago | parent | prev | next [-]

Let's not vague post on HN. What's the problem with the above?

dccsillag 4 hours ago | parent | prev | next [-]

I'm sorry, but what exactly is the problem with the code? I've been staring at it for quite a while now and still don't see what is counterintuitive about it.

dataflow an hour ago | parent | next [-]

Depends on where you're coming from, but some people would expect it to enforce that the pointer is non-null, then proceed. Which would actually give you a guaranteed crash in case it is null. But that's not what it does in C++, and I could see it not being entirely obvious.

IshKebab 34 minutes ago | parent [-]

Assert doesn't work like that in any language.

comex 27 minutes ago | parent [-]

It does in Rust: assert is always enabled, whereas the debug-only version is called debug_assert.

But yes, “assert” in most languages is debug-only.

IshKebab 4 hours ago | parent | prev [-]

There's nothing wrong with it. It does exactly what you think it does when passed null.

an hour ago | parent | prev [-]
[deleted]