1

I was wondering what was the best (I mean performance and proper) of checking an empty value ?

I know these 2 ways :

First (I think the best) :

For any pointer check:

if (value == NULL) ...

For an int:

if (value == 0) ...

Second:

if (value) ...
alk
  • 69,737
  • 10
  • 105
  • 255
zerek
  • 253
  • 1
  • 12

4 Answers4

5
For an Int : if (value == 0)

For int if the value is 0. it doesn't mean that the parameter is empty, it means that the parameter is holding the value 0.

Performance-wise, there is no difference. You can check this by inspecting the compiled assembly code.

The assembly code is given below.

#include<stdio.h>
int main()
{
    int a=0;

    if(a==0)
            printf("hello");

}

main:
.LFB0:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    subq    $16, %rsp
    movl    $0, -4(%rbp)
    cmpl    $0, -4(%rbp)
    jne     .L3
    movl    $.LC0, %edi
    movl    $0, %eax
    call    printf

And for this one,

#include<stdio.h>
int main()
{
    int a=0;

    if(a)
            printf("hello");
}

main:
.LFB0:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    subq    $16, %rsp
    movl    $0, -4(%rbp)
    cmpl    $0, -4(%rbp)
    je      .L3
    movl    $.LC0, %edi
    movl    $0, %eax
    call    printf

You can see that both the codes are same, thus there is no difference performance-wise.

But, as others mentioned, the FIRST one is easier to understand and is more clear. That should be used

Haris
  • 12,120
  • 6
  • 43
  • 70
  • It is a matter of opinion, but I find the first one too verbose for a pointer. My reaction in a code review would be to say "you don't need to do this". For the `int`, I agree that is is good to be explicit, because we are comparing against a particular value, since there is no concept of null for an int. – juanchopanza Aug 27 '15 at 06:54
  • @haris : Thank you very much! – zerek Aug 27 '15 at 08:14
2

You should always use if(value == NULL) since it is the most clear and unambiguous form.

if(value == 0) or if(!value) are common variants, but they are slightly less clear, as you can't tell if the intention was to check a pointer or a variable's value.

There is no performance difference between the 3 above forms and they will yield exactly the same machine code.


Stylistic details:

Stylistically, it is preferred to always treat if statements as if they expect a boolean type (like in C++). Unfortunately C still uses int, rather than a true boolean type, so there exist no type safety between integers and boolean expressions.

But you can write code as if there was a true boolean type, because then you can use external static analysers in order to get a stronger typing for boolean expressions. MISRA-C for example uses a term "essentially boolean" to enforce this.

It is a rather nice and effective way of weeding out obscure expressions and type-related bugs.

In such code if(value) or if(!value) wouldn't be allowed, because value in this example is a pointer, not a bool.

Sources: MISRA-C:2012 rules 10.1, 11.9, 14.4

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • Thank you for these details! – zerek Aug 27 '15 at 08:17
  • I don't agree about the ambiguity, the NULL macro is not guaranteed to be zero. Also, null pointer constant is always the literal 0 (in the source code, though may be translated to something else in machine code). – Veriloud Aug 27 '15 at 14:26
  • 1
    @Veriloud Not correct. The NULL macro is guaranteed to expand to a null pointer constant, which in turn is guaranteed to be either `0` or `(void*)0`. You are probably confusing the NULL macro with _null pointers_, which may be different from zero. [See this](http://stackoverflow.com/questions/32136092/how-to-write-c-c-code-correctly-when-null-pointer-is-not-all-bits-zero). – Lundin Aug 27 '15 at 14:57
  • @Lundin, when you say "guaranteed to expand to a null pointer constant", yes, the preprocessor will expand it to what the language already has direct support for representing a null pointer constant: a literal 0. I meant this is an issue of style as you say, but one could argue that simply using 0 in the first place is less ambiguous. Especially to those working on a machine that use nonzero null pointers. The goal of both styles is the same, to remind us pointers are an abstraction. – Veriloud Aug 27 '15 at 17:48
  • @Lundin, your answer is best, +1, and thanks for the excellent link in your comment reply. Note in the last sentence its preferred answer: "The standard says: A zero value, null pointer value, or null member pointer value is converted to false; So if (p==NULL) and if (!p) does the same thing." That is another reason why some argue if (!p) is more natural to use. – Veriloud Aug 27 '15 at 18:04
1

You should use "First" because it is easier to understand, maintain, ...

There is no noticeable difference performancewise.

DrKoch
  • 9,556
  • 2
  • 34
  • 43
1

Both will perform the same, and will most likely produce the exact same assembly code. Use whatever you see fit.

It's a good idea to consider how your code will be interpreted by other coders. From a readability standpoint if (value == NULL) may seem more clear than if (!value). But it's a matter of style.

PC Luddite
  • 5,883
  • 6
  • 23
  • 39