spir <denis.s...@gmail.com> wrote:

void zeroit(T)(T* ptr) if (!IsPointer!T) {
    memset(ptr, 0, (*ptr).sizeof);
}

Doesn't this in fact hide the error to the programmer (by silently correcting)?

No. It ensures that the pointer and size are from the same pointer.
I would say it is somewhat analogous to D's arrays, which are a hell
of a lot better than free pointer/length pairs.


Why not instead for instance:

void zeroit(T)(T* ptr) if (!IsPointer!T) {
throw new Exception("Type error: argument to <funcname> should be a pointer.");
}

I'm not sure this makes sense. The error message seems to indicate you
don't have a pointer, while that is exactly what you do.
The template constraint also say that this is not an overload of the
above function, making it simply a function that throws.


(And what if the memory to be actually memset is not ptr's target?)

If people start throwing random pointers into functions, that is a
problem that's hard to work around. bearophile's zeroit function takes
memset and removes its main weak point, namely that it takes a free
pointer/length pair, rather than a structure containing the two.


About non-null thingies, I would be all for a mode in which is inserted
        if (p is null) throw ...;
before _every_ implicite or explicite deref of every implicite (pointer) or implicite (class element) pointer. And even make this the default for non-release. (With line number in the message ;-)
Am I dreaming?

Yeah. :P
Much more likely would be the idea that has been discussed, of opt-in
non-nullness, by disabling the default constructor for a struct that
wraps a pointer.

--
Simen

Reply via email to