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) ...
6
  • 1
    Do you want to check for empty string also? Commented Aug 27, 2015 at 6:22
  • value is a very bad name for a pointer. Commented Aug 27, 2015 at 7:04
  • @DrKoch : char* is the same so yes ? Commented Aug 27, 2015 at 8:11
  • @Lundin : I never named pointer "value" don't worry ^^ Commented Aug 27, 2015 at 8:12
  • A check fo no string OR empty string with char *str; would be: if(str == null || str[0] == '\0') Commented Aug 27, 2015 at 8:38

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
@haris : Thank you very much!
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

5 Comments

Thank you for these details!
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 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.
@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.
@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.
1

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

There is no noticeable difference performancewise.

2 Comments

There shouldn't be any performance difference (noticeable or otherwise).
@DrKoch I very much doubt you'll find any such compiler out there.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.