NULL, 0, NULLPTR and nullptr

23 views
Skip to first unread message

Jeffrey Walton

unread,
Mar 2, 2017, 3:16:36 PM3/2/17
to Crypto++ Users
Hi Everyone,

In preparation for GCC 7, we needed to cleanup the use of NULL and 0 values for pointers. Some places we were using 0, some places were were using NULL. All of them lit up like a Christmas tree under GCC 7, which switched to -std=gnu++17 by default. Under C++17, the use of the constant 0 (which NULL is defined as), produced an endless stream of warnings.

Our code now looks like:

   struct C
   {
       C : m_ptr(NULLPTR) {}
       ...
  
       byte* m_ptr;
   };

And then NULLPTR is a macro:

   #if defined(CRYPTOPP_CXX11_NULLPTR) && !defined(NULLPTR)
   # define NULLPTR nullptr
   #elif !defined(NULLPTR)
   # define NULLPTR NULL
   #endif // CRYPTOPP_CXX11_NULLPTR

Should we use nullptr everywhere in the code, and define:

   //
nullptr is not available in C++03, and its not defined. Define it now.
   #if !defined(
CRYPTOPP_CXX11_NULLPTR) && !defined(nullptr)
   #  define
nullptr NULL
   #endif

NULLPTR vs nullptr seems to be mostly aesthetic. However, using nullptr in the source code (as opposed to NULLPTR) will make IDE colorizers highlight it as a literal.

Does anyone have a preference?

Jeffrey Walton

unread,
Mar 2, 2017, 3:31:11 PM3/2/17
to Crypto++ Users


On Thursday, March 2, 2017 at 3:16:36 PM UTC-5, Jeffrey Walton wrote:
Hi Everyone,

In preparation for GCC 7, we needed to cleanup the use of NULL and 0 values for pointers. Some places we were using 0, some places were were using NULL. All of them lit up like a Christmas tree under GCC 7, which switched to -std=gnu++17 by default. Under C++17, the use of the constant 0 (which NULL is defined as), produced an endless stream of warnings.

I should have provided a link to the check-in of interest. This is the check-in that added NULLPTR, and allows us to pivot to NULL (C++03 and below) or nullptr (C++11 and above): https://github.com/weidai11/cryptopp/commit/5efb019d8bdc593b3c1a0b57d615b170c7dab02a.

Jeff

olafv...@gmail.com

unread,
Apr 28, 2017, 4:41:55 PM4/28/17
to Crypto++ Users


Op donderdag 2 maart 2017 21:16:36 UTC+1 schreef Jeffrey Walton:
nullptr is the standard and the future.

 

olafv...@gmail.com

unread,
Apr 28, 2017, 5:12:34 PM4/28/17
to Crypto++ Users


Op donderdag 2 maart 2017 21:16:36 UTC+1 schreef Jeffrey Walton:
which switched to -std=gnu++17 by default.

Did it?

Jeffrey Walton

unread,
Apr 28, 2017, 6:14:15 PM4/28/17
to Crypto++ Users


On Friday, April 28, 2017 at 5:12:34 PM UTC-4, olafv...@gmail.com wrote:

Op donderdag 2 maart 2017 21:16:36 UTC+1 schreef Jeffrey Walton:
which switched to -std=gnu++17 by default.

Did it?

 I'm trying to locate the source now.

I hope I did not conflate the C++17 warning with -std=gnu++17  as default.

Jeff

olafv...@gmail.com

unread,
Apr 30, 2017, 4:27:06 AM4/30/17
to Crypto++ Users


Op zaterdag 29 april 2017 00:14:15 UTC+2 schreef Jeffrey Walton:
I hope I did not conflate the C++17 warning with -std=gnu++17  as default.

I also can't imagine warnings for NULL..

There is "Null pointer constants" but that's something different.
 

Jeffrey Walton

unread,
Apr 30, 2017, 11:22:36 AM4/30/17
to Crypto++ Users


On Sunday, April 30, 2017 at 4:27:06 AM UTC-4, olafv...@gmail.com wrote:

Op zaterdag 29 april 2017 00:14:15 UTC+2 schreef Jeffrey Walton:
I hope I did not conflate the C++17 warning with -std=gnu++17  as default.

I also can't imagine warnings for NULL..

There is "Null pointer constants" but that's something different.

Yes, that was part of the problem. Other pieces include Crypto++ using 0 in some places rather than NULL. Because the 0 was inlined from a header, it multiplied like feral cats and dogs.

Another piece of the problem is we cannot do the following because GCC defines NULL (and redefines it). I'm guessing its a bad practice to mess with the definition of NULL:

    #undef NULL
    #define NULL nullptr_t

Also see https://github.com/weidai11/cryptopp/commit/5efb019d8bdc593b.

Jeff
Reply all
Reply to author
Forward
0 new messages