C++ NULL vs __null












18














I have the following code:



MyType x = NULL;


NetBeans gave me a suggestion to change it to this:



MyType x = __null;


I looked it up and found that __null is called a "compiler keyword", which I assumed to mean it's used internally for the compiler. I don't understand why NetBeans suggested to change it to a compiler keyword.



What's the difference between NULL and __null in c++?










share|improve this question




















  • 40




    In C++11 and after you should use the keyword nullptr.
    – Anon Mail
    2 days ago






  • 5




    Don't use __null. If it's an implementation detail, it's not portable to use it. If it's defined by the project, it's using a name reserved for use by the implementation, it's not legal to use that identifier and it should be removed from the project.
    – François Andrieux
    2 days ago






  • 2




    If you want NULL use nullptr. If that doesn't compile then you don't have a pointer and you are doing the wrong thing.
    – NathanOliver
    2 days ago












  • You want nullptr - always.
    – Jesper Juhl
    2 days ago






  • 1




    Strange suggestion from NetBeans there. Possibly worth raising this with its devs, though we can't tell for sure with the slim pickings of information provided.
    – Lightness Races in Orbit
    yesterday


















18














I have the following code:



MyType x = NULL;


NetBeans gave me a suggestion to change it to this:



MyType x = __null;


I looked it up and found that __null is called a "compiler keyword", which I assumed to mean it's used internally for the compiler. I don't understand why NetBeans suggested to change it to a compiler keyword.



What's the difference between NULL and __null in c++?










share|improve this question




















  • 40




    In C++11 and after you should use the keyword nullptr.
    – Anon Mail
    2 days ago






  • 5




    Don't use __null. If it's an implementation detail, it's not portable to use it. If it's defined by the project, it's using a name reserved for use by the implementation, it's not legal to use that identifier and it should be removed from the project.
    – François Andrieux
    2 days ago






  • 2




    If you want NULL use nullptr. If that doesn't compile then you don't have a pointer and you are doing the wrong thing.
    – NathanOliver
    2 days ago












  • You want nullptr - always.
    – Jesper Juhl
    2 days ago






  • 1




    Strange suggestion from NetBeans there. Possibly worth raising this with its devs, though we can't tell for sure with the slim pickings of information provided.
    – Lightness Races in Orbit
    yesterday
















18












18








18







I have the following code:



MyType x = NULL;


NetBeans gave me a suggestion to change it to this:



MyType x = __null;


I looked it up and found that __null is called a "compiler keyword", which I assumed to mean it's used internally for the compiler. I don't understand why NetBeans suggested to change it to a compiler keyword.



What's the difference between NULL and __null in c++?










share|improve this question















I have the following code:



MyType x = NULL;


NetBeans gave me a suggestion to change it to this:



MyType x = __null;


I looked it up and found that __null is called a "compiler keyword", which I assumed to mean it's used internally for the compiler. I don't understand why NetBeans suggested to change it to a compiler keyword.



What's the difference between NULL and __null in c++?







c++ null keyword






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









Vadim Kotov

4,32153247




4,32153247










asked 2 days ago









David the third

1,2921723




1,2921723








  • 40




    In C++11 and after you should use the keyword nullptr.
    – Anon Mail
    2 days ago






  • 5




    Don't use __null. If it's an implementation detail, it's not portable to use it. If it's defined by the project, it's using a name reserved for use by the implementation, it's not legal to use that identifier and it should be removed from the project.
    – François Andrieux
    2 days ago






  • 2




    If you want NULL use nullptr. If that doesn't compile then you don't have a pointer and you are doing the wrong thing.
    – NathanOliver
    2 days ago












  • You want nullptr - always.
    – Jesper Juhl
    2 days ago






  • 1




    Strange suggestion from NetBeans there. Possibly worth raising this with its devs, though we can't tell for sure with the slim pickings of information provided.
    – Lightness Races in Orbit
    yesterday
















  • 40




    In C++11 and after you should use the keyword nullptr.
    – Anon Mail
    2 days ago






  • 5




    Don't use __null. If it's an implementation detail, it's not portable to use it. If it's defined by the project, it's using a name reserved for use by the implementation, it's not legal to use that identifier and it should be removed from the project.
    – François Andrieux
    2 days ago






  • 2




    If you want NULL use nullptr. If that doesn't compile then you don't have a pointer and you are doing the wrong thing.
    – NathanOliver
    2 days ago












  • You want nullptr - always.
    – Jesper Juhl
    2 days ago






  • 1




    Strange suggestion from NetBeans there. Possibly worth raising this with its devs, though we can't tell for sure with the slim pickings of information provided.
    – Lightness Races in Orbit
    yesterday










40




40




In C++11 and after you should use the keyword nullptr.
– Anon Mail
2 days ago




In C++11 and after you should use the keyword nullptr.
– Anon Mail
2 days ago




5




5




Don't use __null. If it's an implementation detail, it's not portable to use it. If it's defined by the project, it's using a name reserved for use by the implementation, it's not legal to use that identifier and it should be removed from the project.
– François Andrieux
2 days ago




Don't use __null. If it's an implementation detail, it's not portable to use it. If it's defined by the project, it's using a name reserved for use by the implementation, it's not legal to use that identifier and it should be removed from the project.
– François Andrieux
2 days ago




2




2




If you want NULL use nullptr. If that doesn't compile then you don't have a pointer and you are doing the wrong thing.
– NathanOliver
2 days ago






If you want NULL use nullptr. If that doesn't compile then you don't have a pointer and you are doing the wrong thing.
– NathanOliver
2 days ago














You want nullptr - always.
– Jesper Juhl
2 days ago




You want nullptr - always.
– Jesper Juhl
2 days ago




1




1




Strange suggestion from NetBeans there. Possibly worth raising this with its devs, though we can't tell for sure with the slim pickings of information provided.
– Lightness Races in Orbit
yesterday






Strange suggestion from NetBeans there. Possibly worth raising this with its devs, though we can't tell for sure with the slim pickings of information provided.
– Lightness Races in Orbit
yesterday














3 Answers
3






active

oldest

votes


















40














__null is a g++ internal thing that serves roughly the same purpose as the standard nullptr added in C++11 (acting consistently as a pointer, never an integer).



NULL is defined as 0, which can be implicitly used as integer, boolean, floating point value or pointer, which is a problem when it comes to overload resolution, when you want to call the function that takes a pointer specifically.



In any event, you shouldn't use __null because it's a g++ implementation detail, so using it guarantees non-portable code. If you can rely on C++11 (surely you can by now?), use nullptr. If not, NULL is your only portable option.






share|improve this answer

















  • 10




    Note that NULL does not have to be defined as 0. It's whatever the implementation chooses as a null-pointer constant.
    – Pete Becker
    2 days ago






  • 1




    @PeteBecker: True. It's just that pre-C++11, I don't know of any systems that didn't use 0. In the C++11 era, they're allowed to make it evaluate to nullptr, but I'm not sure which compilers, if any, have pulled that particular trigger (since it could conceivably break existing code).
    – ShadowRanger
    2 days ago






  • 2




    @PeteBecker I think there's some confusion here between 0 as the address of the pointer, and the literal 0 in source code. The latter is defined as yielding a null pointer when it is assigned to a pointer, even though the resulting address is implementation defined.
    – David Conrad
    2 days ago






  • 1




    @ShadowRanger NULL is sometimes defined as 0, sometimes as (void *) 0, depending on the compiler.
    – David Conrad
    2 days ago






  • 3




    @DavidConrad — (void*)0 is a valid null pointer constant in C but not in C++.
    – Pete Becker
    2 days ago



















7














NULL is the old C symbol for a null pointer. C++ traditionally have used 0 for null pointers, and since the C++11 standard nullptr.



Considering that x doesn't seem to be a pointer then you can't initialize x to be a null pointer, and the __null symbol is perhaps some compiler-internal symbol for a null value (which is a concept that doesn't really exist in standard C++).



If you want x to initialized to some default state, then you have to rely on the MyClass default constructor to initialize the objects and its member variables to some suitable default values.






share|improve this answer





























    7














    NULL has been overtaken from C into C++ and - prior to C++11 - adopted its C meaning:




    until C++11: The macro NULL is an implementation-defined null pointer
    constant, which may be an integral constant expression rvalue of
    integer type that evaluates to zero.




    C++11 then introduced a dedicated null pointer literal nullptr of type std::nullptr_t. But - probably for backward compatibility - the macro NULL was not removed; its definition was just a bit relaxed in that compilers may now define it either as integral or as pointer type:




    C++11 onwards: an integer literal with value zero, or a prvalue of
    type std::nullptr_t




    If you use NULL, then you get implementation-defined behaviour in overload resolution. Consider, for example, the following code with a compiler that uses the integral-version of NULL-macro. Then a call using NULL as parameter passed to a function may lead to ambiguities:



    struct SomeOverload {

    SomeOverload(int x) {
    cout << "taking int param: " << x << endl;
    }
    SomeOverload(void* x) {
    cout << "taking void* param: " << x << endl;
    }
    };

    int main() {

    int someVal = 10;

    SomeOverload a(0);
    SomeOverload b(&someVal);

    // SomeOverload c(NULL); // Call to constructor is ambiuous
    SomeOverload d(nullptr);
    }


    So it is recommended to use nullptr where ever you want to express pointer type.



    And don't use __null, as this is a compiler-specific, non-portable constant; nullptr, in contrast, is perfectly portable.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53963646%2fc-null-vs-null%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      40














      __null is a g++ internal thing that serves roughly the same purpose as the standard nullptr added in C++11 (acting consistently as a pointer, never an integer).



      NULL is defined as 0, which can be implicitly used as integer, boolean, floating point value or pointer, which is a problem when it comes to overload resolution, when you want to call the function that takes a pointer specifically.



      In any event, you shouldn't use __null because it's a g++ implementation detail, so using it guarantees non-portable code. If you can rely on C++11 (surely you can by now?), use nullptr. If not, NULL is your only portable option.






      share|improve this answer

















      • 10




        Note that NULL does not have to be defined as 0. It's whatever the implementation chooses as a null-pointer constant.
        – Pete Becker
        2 days ago






      • 1




        @PeteBecker: True. It's just that pre-C++11, I don't know of any systems that didn't use 0. In the C++11 era, they're allowed to make it evaluate to nullptr, but I'm not sure which compilers, if any, have pulled that particular trigger (since it could conceivably break existing code).
        – ShadowRanger
        2 days ago






      • 2




        @PeteBecker I think there's some confusion here between 0 as the address of the pointer, and the literal 0 in source code. The latter is defined as yielding a null pointer when it is assigned to a pointer, even though the resulting address is implementation defined.
        – David Conrad
        2 days ago






      • 1




        @ShadowRanger NULL is sometimes defined as 0, sometimes as (void *) 0, depending on the compiler.
        – David Conrad
        2 days ago






      • 3




        @DavidConrad — (void*)0 is a valid null pointer constant in C but not in C++.
        – Pete Becker
        2 days ago
















      40














      __null is a g++ internal thing that serves roughly the same purpose as the standard nullptr added in C++11 (acting consistently as a pointer, never an integer).



      NULL is defined as 0, which can be implicitly used as integer, boolean, floating point value or pointer, which is a problem when it comes to overload resolution, when you want to call the function that takes a pointer specifically.



      In any event, you shouldn't use __null because it's a g++ implementation detail, so using it guarantees non-portable code. If you can rely on C++11 (surely you can by now?), use nullptr. If not, NULL is your only portable option.






      share|improve this answer

















      • 10




        Note that NULL does not have to be defined as 0. It's whatever the implementation chooses as a null-pointer constant.
        – Pete Becker
        2 days ago






      • 1




        @PeteBecker: True. It's just that pre-C++11, I don't know of any systems that didn't use 0. In the C++11 era, they're allowed to make it evaluate to nullptr, but I'm not sure which compilers, if any, have pulled that particular trigger (since it could conceivably break existing code).
        – ShadowRanger
        2 days ago






      • 2




        @PeteBecker I think there's some confusion here between 0 as the address of the pointer, and the literal 0 in source code. The latter is defined as yielding a null pointer when it is assigned to a pointer, even though the resulting address is implementation defined.
        – David Conrad
        2 days ago






      • 1




        @ShadowRanger NULL is sometimes defined as 0, sometimes as (void *) 0, depending on the compiler.
        – David Conrad
        2 days ago






      • 3




        @DavidConrad — (void*)0 is a valid null pointer constant in C but not in C++.
        – Pete Becker
        2 days ago














      40












      40








      40






      __null is a g++ internal thing that serves roughly the same purpose as the standard nullptr added in C++11 (acting consistently as a pointer, never an integer).



      NULL is defined as 0, which can be implicitly used as integer, boolean, floating point value or pointer, which is a problem when it comes to overload resolution, when you want to call the function that takes a pointer specifically.



      In any event, you shouldn't use __null because it's a g++ implementation detail, so using it guarantees non-portable code. If you can rely on C++11 (surely you can by now?), use nullptr. If not, NULL is your only portable option.






      share|improve this answer












      __null is a g++ internal thing that serves roughly the same purpose as the standard nullptr added in C++11 (acting consistently as a pointer, never an integer).



      NULL is defined as 0, which can be implicitly used as integer, boolean, floating point value or pointer, which is a problem when it comes to overload resolution, when you want to call the function that takes a pointer specifically.



      In any event, you shouldn't use __null because it's a g++ implementation detail, so using it guarantees non-portable code. If you can rely on C++11 (surely you can by now?), use nullptr. If not, NULL is your only portable option.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered 2 days ago









      ShadowRanger

      57.5k45395




      57.5k45395








      • 10




        Note that NULL does not have to be defined as 0. It's whatever the implementation chooses as a null-pointer constant.
        – Pete Becker
        2 days ago






      • 1




        @PeteBecker: True. It's just that pre-C++11, I don't know of any systems that didn't use 0. In the C++11 era, they're allowed to make it evaluate to nullptr, but I'm not sure which compilers, if any, have pulled that particular trigger (since it could conceivably break existing code).
        – ShadowRanger
        2 days ago






      • 2




        @PeteBecker I think there's some confusion here between 0 as the address of the pointer, and the literal 0 in source code. The latter is defined as yielding a null pointer when it is assigned to a pointer, even though the resulting address is implementation defined.
        – David Conrad
        2 days ago






      • 1




        @ShadowRanger NULL is sometimes defined as 0, sometimes as (void *) 0, depending on the compiler.
        – David Conrad
        2 days ago






      • 3




        @DavidConrad — (void*)0 is a valid null pointer constant in C but not in C++.
        – Pete Becker
        2 days ago














      • 10




        Note that NULL does not have to be defined as 0. It's whatever the implementation chooses as a null-pointer constant.
        – Pete Becker
        2 days ago






      • 1




        @PeteBecker: True. It's just that pre-C++11, I don't know of any systems that didn't use 0. In the C++11 era, they're allowed to make it evaluate to nullptr, but I'm not sure which compilers, if any, have pulled that particular trigger (since it could conceivably break existing code).
        – ShadowRanger
        2 days ago






      • 2




        @PeteBecker I think there's some confusion here between 0 as the address of the pointer, and the literal 0 in source code. The latter is defined as yielding a null pointer when it is assigned to a pointer, even though the resulting address is implementation defined.
        – David Conrad
        2 days ago






      • 1




        @ShadowRanger NULL is sometimes defined as 0, sometimes as (void *) 0, depending on the compiler.
        – David Conrad
        2 days ago






      • 3




        @DavidConrad — (void*)0 is a valid null pointer constant in C but not in C++.
        – Pete Becker
        2 days ago








      10




      10




      Note that NULL does not have to be defined as 0. It's whatever the implementation chooses as a null-pointer constant.
      – Pete Becker
      2 days ago




      Note that NULL does not have to be defined as 0. It's whatever the implementation chooses as a null-pointer constant.
      – Pete Becker
      2 days ago




      1




      1




      @PeteBecker: True. It's just that pre-C++11, I don't know of any systems that didn't use 0. In the C++11 era, they're allowed to make it evaluate to nullptr, but I'm not sure which compilers, if any, have pulled that particular trigger (since it could conceivably break existing code).
      – ShadowRanger
      2 days ago




      @PeteBecker: True. It's just that pre-C++11, I don't know of any systems that didn't use 0. In the C++11 era, they're allowed to make it evaluate to nullptr, but I'm not sure which compilers, if any, have pulled that particular trigger (since it could conceivably break existing code).
      – ShadowRanger
      2 days ago




      2




      2




      @PeteBecker I think there's some confusion here between 0 as the address of the pointer, and the literal 0 in source code. The latter is defined as yielding a null pointer when it is assigned to a pointer, even though the resulting address is implementation defined.
      – David Conrad
      2 days ago




      @PeteBecker I think there's some confusion here between 0 as the address of the pointer, and the literal 0 in source code. The latter is defined as yielding a null pointer when it is assigned to a pointer, even though the resulting address is implementation defined.
      – David Conrad
      2 days ago




      1




      1




      @ShadowRanger NULL is sometimes defined as 0, sometimes as (void *) 0, depending on the compiler.
      – David Conrad
      2 days ago




      @ShadowRanger NULL is sometimes defined as 0, sometimes as (void *) 0, depending on the compiler.
      – David Conrad
      2 days ago




      3




      3




      @DavidConrad — (void*)0 is a valid null pointer constant in C but not in C++.
      – Pete Becker
      2 days ago




      @DavidConrad — (void*)0 is a valid null pointer constant in C but not in C++.
      – Pete Becker
      2 days ago













      7














      NULL is the old C symbol for a null pointer. C++ traditionally have used 0 for null pointers, and since the C++11 standard nullptr.



      Considering that x doesn't seem to be a pointer then you can't initialize x to be a null pointer, and the __null symbol is perhaps some compiler-internal symbol for a null value (which is a concept that doesn't really exist in standard C++).



      If you want x to initialized to some default state, then you have to rely on the MyClass default constructor to initialize the objects and its member variables to some suitable default values.






      share|improve this answer


























        7














        NULL is the old C symbol for a null pointer. C++ traditionally have used 0 for null pointers, and since the C++11 standard nullptr.



        Considering that x doesn't seem to be a pointer then you can't initialize x to be a null pointer, and the __null symbol is perhaps some compiler-internal symbol for a null value (which is a concept that doesn't really exist in standard C++).



        If you want x to initialized to some default state, then you have to rely on the MyClass default constructor to initialize the objects and its member variables to some suitable default values.






        share|improve this answer
























          7












          7








          7






          NULL is the old C symbol for a null pointer. C++ traditionally have used 0 for null pointers, and since the C++11 standard nullptr.



          Considering that x doesn't seem to be a pointer then you can't initialize x to be a null pointer, and the __null symbol is perhaps some compiler-internal symbol for a null value (which is a concept that doesn't really exist in standard C++).



          If you want x to initialized to some default state, then you have to rely on the MyClass default constructor to initialize the objects and its member variables to some suitable default values.






          share|improve this answer












          NULL is the old C symbol for a null pointer. C++ traditionally have used 0 for null pointers, and since the C++11 standard nullptr.



          Considering that x doesn't seem to be a pointer then you can't initialize x to be a null pointer, and the __null symbol is perhaps some compiler-internal symbol for a null value (which is a concept that doesn't really exist in standard C++).



          If you want x to initialized to some default state, then you have to rely on the MyClass default constructor to initialize the objects and its member variables to some suitable default values.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 days ago









          Some programmer dude

          294k24248410




          294k24248410























              7














              NULL has been overtaken from C into C++ and - prior to C++11 - adopted its C meaning:




              until C++11: The macro NULL is an implementation-defined null pointer
              constant, which may be an integral constant expression rvalue of
              integer type that evaluates to zero.




              C++11 then introduced a dedicated null pointer literal nullptr of type std::nullptr_t. But - probably for backward compatibility - the macro NULL was not removed; its definition was just a bit relaxed in that compilers may now define it either as integral or as pointer type:




              C++11 onwards: an integer literal with value zero, or a prvalue of
              type std::nullptr_t




              If you use NULL, then you get implementation-defined behaviour in overload resolution. Consider, for example, the following code with a compiler that uses the integral-version of NULL-macro. Then a call using NULL as parameter passed to a function may lead to ambiguities:



              struct SomeOverload {

              SomeOverload(int x) {
              cout << "taking int param: " << x << endl;
              }
              SomeOverload(void* x) {
              cout << "taking void* param: " << x << endl;
              }
              };

              int main() {

              int someVal = 10;

              SomeOverload a(0);
              SomeOverload b(&someVal);

              // SomeOverload c(NULL); // Call to constructor is ambiuous
              SomeOverload d(nullptr);
              }


              So it is recommended to use nullptr where ever you want to express pointer type.



              And don't use __null, as this is a compiler-specific, non-portable constant; nullptr, in contrast, is perfectly portable.






              share|improve this answer




























                7














                NULL has been overtaken from C into C++ and - prior to C++11 - adopted its C meaning:




                until C++11: The macro NULL is an implementation-defined null pointer
                constant, which may be an integral constant expression rvalue of
                integer type that evaluates to zero.




                C++11 then introduced a dedicated null pointer literal nullptr of type std::nullptr_t. But - probably for backward compatibility - the macro NULL was not removed; its definition was just a bit relaxed in that compilers may now define it either as integral or as pointer type:




                C++11 onwards: an integer literal with value zero, or a prvalue of
                type std::nullptr_t




                If you use NULL, then you get implementation-defined behaviour in overload resolution. Consider, for example, the following code with a compiler that uses the integral-version of NULL-macro. Then a call using NULL as parameter passed to a function may lead to ambiguities:



                struct SomeOverload {

                SomeOverload(int x) {
                cout << "taking int param: " << x << endl;
                }
                SomeOverload(void* x) {
                cout << "taking void* param: " << x << endl;
                }
                };

                int main() {

                int someVal = 10;

                SomeOverload a(0);
                SomeOverload b(&someVal);

                // SomeOverload c(NULL); // Call to constructor is ambiuous
                SomeOverload d(nullptr);
                }


                So it is recommended to use nullptr where ever you want to express pointer type.



                And don't use __null, as this is a compiler-specific, non-portable constant; nullptr, in contrast, is perfectly portable.






                share|improve this answer


























                  7












                  7








                  7






                  NULL has been overtaken from C into C++ and - prior to C++11 - adopted its C meaning:




                  until C++11: The macro NULL is an implementation-defined null pointer
                  constant, which may be an integral constant expression rvalue of
                  integer type that evaluates to zero.




                  C++11 then introduced a dedicated null pointer literal nullptr of type std::nullptr_t. But - probably for backward compatibility - the macro NULL was not removed; its definition was just a bit relaxed in that compilers may now define it either as integral or as pointer type:




                  C++11 onwards: an integer literal with value zero, or a prvalue of
                  type std::nullptr_t




                  If you use NULL, then you get implementation-defined behaviour in overload resolution. Consider, for example, the following code with a compiler that uses the integral-version of NULL-macro. Then a call using NULL as parameter passed to a function may lead to ambiguities:



                  struct SomeOverload {

                  SomeOverload(int x) {
                  cout << "taking int param: " << x << endl;
                  }
                  SomeOverload(void* x) {
                  cout << "taking void* param: " << x << endl;
                  }
                  };

                  int main() {

                  int someVal = 10;

                  SomeOverload a(0);
                  SomeOverload b(&someVal);

                  // SomeOverload c(NULL); // Call to constructor is ambiuous
                  SomeOverload d(nullptr);
                  }


                  So it is recommended to use nullptr where ever you want to express pointer type.



                  And don't use __null, as this is a compiler-specific, non-portable constant; nullptr, in contrast, is perfectly portable.






                  share|improve this answer














                  NULL has been overtaken from C into C++ and - prior to C++11 - adopted its C meaning:




                  until C++11: The macro NULL is an implementation-defined null pointer
                  constant, which may be an integral constant expression rvalue of
                  integer type that evaluates to zero.




                  C++11 then introduced a dedicated null pointer literal nullptr of type std::nullptr_t. But - probably for backward compatibility - the macro NULL was not removed; its definition was just a bit relaxed in that compilers may now define it either as integral or as pointer type:




                  C++11 onwards: an integer literal with value zero, or a prvalue of
                  type std::nullptr_t




                  If you use NULL, then you get implementation-defined behaviour in overload resolution. Consider, for example, the following code with a compiler that uses the integral-version of NULL-macro. Then a call using NULL as parameter passed to a function may lead to ambiguities:



                  struct SomeOverload {

                  SomeOverload(int x) {
                  cout << "taking int param: " << x << endl;
                  }
                  SomeOverload(void* x) {
                  cout << "taking void* param: " << x << endl;
                  }
                  };

                  int main() {

                  int someVal = 10;

                  SomeOverload a(0);
                  SomeOverload b(&someVal);

                  // SomeOverload c(NULL); // Call to constructor is ambiuous
                  SomeOverload d(nullptr);
                  }


                  So it is recommended to use nullptr where ever you want to express pointer type.



                  And don't use __null, as this is a compiler-specific, non-portable constant; nullptr, in contrast, is perfectly portable.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 2 days ago









                  ilkkachu

                  3,394317




                  3,394317










                  answered 2 days ago









                  Stephan Lechner

                  25.8k21839




                  25.8k21839






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53963646%2fc-null-vs-null%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                      Alcedinidae

                      Origin of the phrase “under your belt”?