-3

I am calling a service which has some optional parameters.

I am setting the optional parameters as NULL . But when i check the logs at service side, parameters passed as NULL have 0 numerical value.

Any idea why its happening and any way to prevent this ?

  • 1
    Because actually [NULL actually is a - macro with - numeric value](http://stackoverflow.com/questions/12023476/what-header-defines-null-in-c) – Adriano Repetti Oct 13 '14 at 07:55

3 Answers3

1

This is because NULL is usually set to 0 (on some systems it might be 0L). If you're using C++11/14, use nullptr instead.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
1

NULL is defined as 0. It stands in for a null pointer.

Since it's a preprocessor definition, the run-time will not have access to the original token.

(The current C++ standard has nullptr instead. That just might be emitted into the runtime if you use it, depending on the capabilities of your debugger. As an important final remark, note that none of this implies that the actual address is zero, it's just the value that the null pointer chooses to take.)

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • This is true for many compilers, but the standard only requires `NULL` to be a "null pointer constant", which is an integral constant expression evaluating to 0. At least one compiler defines it as `__nullptr`, a compiler built-in which evaluates to an integral `0`, but which is a separate token which survives pre-processing, and will cause a warning if it isn't immediately converted to a pointer type. – James Kanze Oct 13 '14 at 09:32
  • `std::cout << nullptr` is ambiguous. The conversions to `void const*` and `char const*` are equally good. And I don't think you can legally add an overload for `operator<<( std::ostream&, std::nullptr_t )` (but I'm not sure). – James Kanze Oct 13 '14 at 09:37
0

I don't believe there is any way to pass a C++ standard "null value" (0, NULL or nullptr) to a function and distinguish one from another. It will be output as zero either way. In other words, NULL is defined to be 0 in the C++ standard, and 0 and nullptr are interchangeable with regards to value - with only the difference that nullptr can only be assigned to something that is a pointer.

I wrote this code, and it outputs p=0 q=0 for the first line, and p = zero and q = zero for the second line.

#include <cstddef>
#include <iostream>

using namespace std;

int main()
{
    int *p = nullptr;
    int *q = NULL;

    cout << "p=" << p << " q=" << q << endl;

    if (q == 0)
    {
        cout << "q = zero" << endl;
    }

    if (p == 0)
    {
        cout << "p = zero" << endl;
    }
}

If you want to, you could of course write your own translation for 0 to print NULL instead. Something like:

  if (p) 
     cout << p;
  else
     cout << "NULL"; 

But that gets a bit tedious, so I would just accept that NULL and 0 are the same thing.

(In code I write, I just use 0, not NULL or nullptr)

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227