Null references redux
Michel Fortin
michel.fortin at michelf.com
Wed Sep 30 14:15:23 PDT 2009
On 2009-09-30 15:30:02 -0400, "Denis Koroskin" <2korden at gmail.com> said:
> Note that C stdlib (and other libraries/bindings) will need to be
> updated to reflect changes, e.g.
>
> extern(C) void*? malloc(size_t size); // may return null!
>
> which is great because it will provide additional safety. I've seen
> quite a lot of code that don't test returned value against null (which
> is a mistake, I believe).
Which makes me think of this: pointers being non-nullable by default
will make it easy to make mistakes when writing C bindings. A
programmer might see this C declaration:
void* malloc(size_t size);
and naively translate it to D like this:
extern(C) void* malloc(size_t size);
without noticing the change in semantics.
For pointer arguments it's not much of a problem: the worse that can
happen is that it blocks you from passing a null value when you should
(in which case you can update the bindings). For a return value it's
more troublesome because you're implicitly adding a promise that the
function will not return null, and you might not realize it's wrong
until it does indeed return null and your program crashes with a
segfault.
Not that I think it's worth bothering too much, but it's something to
keep in mind.
--
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/
More information about the Digitalmars-d
mailing list