C++ NULL vs __null

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












25














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



















  • 42




    In C++11 and after you should use the keyword nullptr.
    – Anon Mail
    Dec 28 '18 at 19:51






  • 6




    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
    Dec 28 '18 at 19:52






  • 3




    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
    Dec 28 '18 at 19:53











  • You want nullptr - always.
    – Jesper Juhl
    Dec 28 '18 at 19:57






  • 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
    Dec 29 '18 at 18:39
















25














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



















  • 42




    In C++11 and after you should use the keyword nullptr.
    – Anon Mail
    Dec 28 '18 at 19:51






  • 6




    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
    Dec 28 '18 at 19:52






  • 3




    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
    Dec 28 '18 at 19:53











  • You want nullptr - always.
    – Jesper Juhl
    Dec 28 '18 at 19:57






  • 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
    Dec 29 '18 at 18:39














25












25








25







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 Dec 29 '18 at 8:18









Vadim Kotov

4,37053247




4,37053247










asked Dec 28 '18 at 19:49









pizza static void mainpizza static void main

1,5341924




1,5341924







  • 42




    In C++11 and after you should use the keyword nullptr.
    – Anon Mail
    Dec 28 '18 at 19:51






  • 6




    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
    Dec 28 '18 at 19:52






  • 3




    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
    Dec 28 '18 at 19:53











  • You want nullptr - always.
    – Jesper Juhl
    Dec 28 '18 at 19:57






  • 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
    Dec 29 '18 at 18:39













  • 42




    In C++11 and after you should use the keyword nullptr.
    – Anon Mail
    Dec 28 '18 at 19:51






  • 6




    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
    Dec 28 '18 at 19:52






  • 3




    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
    Dec 28 '18 at 19:53











  • You want nullptr - always.
    – Jesper Juhl
    Dec 28 '18 at 19:57






  • 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
    Dec 29 '18 at 18:39








42




42




In C++11 and after you should use the keyword nullptr.
– Anon Mail
Dec 28 '18 at 19:51




In C++11 and after you should use the keyword nullptr.
– Anon Mail
Dec 28 '18 at 19:51




6




6




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
Dec 28 '18 at 19:52




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
Dec 28 '18 at 19:52




3




3




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
Dec 28 '18 at 19:53





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
Dec 28 '18 at 19:53













You want nullptr - always.
– Jesper Juhl
Dec 28 '18 at 19:57




You want nullptr - always.
– Jesper Juhl
Dec 28 '18 at 19:57




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
Dec 29 '18 at 18:39





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
Dec 29 '18 at 18:39













3 Answers
3






active

oldest

votes


















47














__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
















  • 13




    Note that NULL does not have to be defined as 0. It's whatever the implementation chooses as a null-pointer constant.
    – Pete Becker
    Dec 28 '18 at 20:18






  • 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
    Dec 29 '18 at 1:09






  • 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
    Dec 29 '18 at 2:28






  • 1




    @ShadowRanger NULL is sometimes defined as 0, sometimes as (void *) 0, depending on the compiler.
    – David Conrad
    Dec 29 '18 at 2:30






  • 5




    @DavidConrad — (void*)0 is a valid null pointer constant in C but not in C++.
    – Pete Becker
    Dec 29 '18 at 3:10


















8














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




























    8














    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









      47














      __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
















      • 13




        Note that NULL does not have to be defined as 0. It's whatever the implementation chooses as a null-pointer constant.
        – Pete Becker
        Dec 28 '18 at 20:18






      • 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
        Dec 29 '18 at 1:09






      • 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
        Dec 29 '18 at 2:28






      • 1




        @ShadowRanger NULL is sometimes defined as 0, sometimes as (void *) 0, depending on the compiler.
        – David Conrad
        Dec 29 '18 at 2:30






      • 5




        @DavidConrad — (void*)0 is a valid null pointer constant in C but not in C++.
        – Pete Becker
        Dec 29 '18 at 3:10















      47














      __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
















      • 13




        Note that NULL does not have to be defined as 0. It's whatever the implementation chooses as a null-pointer constant.
        – Pete Becker
        Dec 28 '18 at 20:18






      • 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
        Dec 29 '18 at 1:09






      • 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
        Dec 29 '18 at 2:28






      • 1




        @ShadowRanger NULL is sometimes defined as 0, sometimes as (void *) 0, depending on the compiler.
        – David Conrad
        Dec 29 '18 at 2:30






      • 5




        @DavidConrad — (void*)0 is a valid null pointer constant in C but not in C++.
        – Pete Becker
        Dec 29 '18 at 3:10













      47












      47








      47






      __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 Dec 28 '18 at 19:54









      ShadowRangerShadowRanger

      58.5k45495




      58.5k45495







      • 13




        Note that NULL does not have to be defined as 0. It's whatever the implementation chooses as a null-pointer constant.
        – Pete Becker
        Dec 28 '18 at 20:18






      • 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
        Dec 29 '18 at 1:09






      • 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
        Dec 29 '18 at 2:28






      • 1




        @ShadowRanger NULL is sometimes defined as 0, sometimes as (void *) 0, depending on the compiler.
        – David Conrad
        Dec 29 '18 at 2:30






      • 5




        @DavidConrad — (void*)0 is a valid null pointer constant in C but not in C++.
        – Pete Becker
        Dec 29 '18 at 3:10












      • 13




        Note that NULL does not have to be defined as 0. It's whatever the implementation chooses as a null-pointer constant.
        – Pete Becker
        Dec 28 '18 at 20:18






      • 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
        Dec 29 '18 at 1:09






      • 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
        Dec 29 '18 at 2:28






      • 1




        @ShadowRanger NULL is sometimes defined as 0, sometimes as (void *) 0, depending on the compiler.
        – David Conrad
        Dec 29 '18 at 2:30






      • 5




        @DavidConrad — (void*)0 is a valid null pointer constant in C but not in C++.
        – Pete Becker
        Dec 29 '18 at 3:10







      13




      13




      Note that NULL does not have to be defined as 0. It's whatever the implementation chooses as a null-pointer constant.
      – Pete Becker
      Dec 28 '18 at 20:18




      Note that NULL does not have to be defined as 0. It's whatever the implementation chooses as a null-pointer constant.
      – Pete Becker
      Dec 28 '18 at 20:18




      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
      Dec 29 '18 at 1:09




      @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
      Dec 29 '18 at 1:09




      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
      Dec 29 '18 at 2:28




      @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
      Dec 29 '18 at 2:28




      1




      1




      @ShadowRanger NULL is sometimes defined as 0, sometimes as (void *) 0, depending on the compiler.
      – David Conrad
      Dec 29 '18 at 2:30




      @ShadowRanger NULL is sometimes defined as 0, sometimes as (void *) 0, depending on the compiler.
      – David Conrad
      Dec 29 '18 at 2:30




      5




      5




      @DavidConrad — (void*)0 is a valid null pointer constant in C but not in C++.
      – Pete Becker
      Dec 29 '18 at 3:10




      @DavidConrad — (void*)0 is a valid null pointer constant in C but not in C++.
      – Pete Becker
      Dec 29 '18 at 3:10













      8














      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

























        8














        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























          8












          8








          8






          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 Dec 28 '18 at 19:56









          Some programmer dudeSome programmer dude

          295k24249411




          295k24249411





















              8














              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



























                8














                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

























                  8












                  8








                  8






                  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 Dec 29 '18 at 11:25









                  ilkkachu

                  3,404318




                  3,404318










                  answered Dec 28 '18 at 20:23









                  Stephan LechnerStephan Lechner

                  26.5k21939




                  26.5k21939



























                      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.




                      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

                      Peggy Mitchell

                      Palaiologos

                      The Forum (Inglewood, California)