Thread overview
[std.c.stdlib] (malloc(something) is null) or (malloc(something) == 0)?
May 20, 2014
Alexandr Druzhinin
May 20, 2014
bearophile
May 20, 2014
Adam D. Ruppe
May 20, 2014
bearophile
May 20, 2014
In D code I do
void* data = GC.malloc(...);
if(data is null)
	...

In C code I do
void* data = malloc(...);
if(data == null)
	...

What to do when in D code I have
void* data = std.c.stdlib.malloc(...);
if(data ?) // is null vs == 0
May 20, 2014
Alexandr Druzhinin:

> In D code I do
> void* data = GC.malloc(...);
> if(data is null)
> 	...
>
> In C code I do
> void* data = malloc(...);
> if(data == null)
> 	...
>
> What to do when in D code I have
> void* data = std.c.stdlib.malloc(...);
> if(data ?) // is null vs == 0

"x is null" or "x == null" are the same operation when x is a raw pointer.

Bye,
bearophile
May 20, 2014
On Tuesday, 20 May 2014 at 14:03:17 UTC, Alexandr Druzhinin wrote:
> if(data ?) // is null vs == 0

Both would work and do the same thing, but I prefer "is null" because that is most consistent with other D code (where there might be a difference between the two).
May 20, 2014
Adam D. Ruppe:

> but I prefer "is null" because that is most consistent with other D code (where there might be a difference between the two).

Curiously I do the opposite, I use == to remind me it's a pointer :-)

Bye,
bearophile