Thread overview | ||||||
---|---|---|---|---|---|---|
|
May 20, 2014 [std.c.stdlib] (malloc(something) is null) or (malloc(something) == 0)? | ||||
---|---|---|---|---|
| ||||
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 Re: [std.c.stdlib] (malloc(something) is null) or (malloc(something) == 0)? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alexandr Druzhinin | 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 Re: [std.c.stdlib] (malloc(something) is null) or (malloc(something) == 0)? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alexandr Druzhinin | 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 Re: [std.c.stdlib] (malloc(something) is null) or (malloc(something) == 0)? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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
|
Copyright © 1999-2021 by the D Language Foundation