www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - [std.c.stdlib] (malloc(something) is null) or (malloc(something)

reply Alexandr Druzhinin <drug2004 bk.ru> writes:
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
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
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
prev sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
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
parent "bearophile" <bearophileHUGS lycos.com> writes:
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
May 20 2014