Rule 4-10-2 NULL is not a c++ standard

Moderators: david ward, misra cpp

minhyuk
Posts: 5
Joined: Wed Feb 06, 2013 1:51 am
Company: Suresoft Technologies

Rule 4-10-2 NULL is not a c++ standard

Postby minhyuk » Fri Feb 07, 2014 7:34 am

In C++ Standard, There are not exist NULL(its C standard).
So, Programmer should to know about this and Use constant 0 instead of NULL macro.

misra cpp
Posts: 145
Joined: Mon Jun 02, 2008 1:55 pm
Company: MISRA

Re: Rule 4-10-2 NULL is not a c++ standard

Postby misra cpp » Mon Oct 05, 2015 2:47 pm

NULL is defined in C++ (see 4.10 of the Language Reference Manual).

Note that 4-10-2 does not ban the use of the token "NULL", but it does prohibit the use of literal "0" as a null pointer.
Posted by and on behalf of
the MISRA C++ Working Group

minhyuk
Posts: 5
Joined: Wed Feb 06, 2013 1:51 am
Company: Suresoft Technologies

Re: Rule 4-10-2 NULL is not a c++ standard

Postby minhyuk » Fri Jan 06, 2017 6:07 am

misra cpp wrote:NULL is defined in C++ (see 4.10 of the Language Reference Manual).

Note that 4-10-2 does not ban the use of the token "NULL", but it does prohibit the use of literal "0" as a null pointer.


according to ISO-IEC-14882(2003)
C.2.2.3 Macro NULL [diff.null]
1 The macro NULL, defined in any of <clocale>, <cstddef>, <cstdio>, <cstdlib>, <cstring>,
<ctime>, or <cwchar>, is an implementation-defined C + + null pointer constant in this International
Standard (18.1).

According to 4.10 of the C++ standard, a null pointer constant is defined as an integer type of rvalue that is zero.
In order to use NULL, special headers must be included in the code, and 0 is defined as a null pointer constant in the C ++ standard, so I think it is more correct to use 0 rather than NULL.

dg1980
Posts: 108
Joined: Wed Apr 27, 2016 2:33 pm
Company: Elektrobit Automotive GmbH

Re: Rule 4-10-2 NULL is not a c++ standard

Postby dg1980 » Mon Jan 09, 2017 6:30 am

minhyuk wrote:
misra cpp wrote:NULL is defined in C++ (see 4.10 of the Language Reference Manual).

Note that 4-10-2 does not ban the use of the token "NULL", but it does prohibit the use of literal "0" as a null pointer.


according to ISO-IEC-14882(2003)
C.2.2.3 Macro NULL [diff.null]
1 The macro NULL, defined in any of <clocale>, <cstddef>, <cstdio>, <cstdlib>, <cstring>,
<ctime>, or <cwchar>, is an implementation-defined C + + null pointer constant in this International
Standard (18.1).

According to 4.10 of the C++ standard, a null pointer constant is defined as an integer type of rvalue that is zero.
In order to use NULL, special headers must be included in the code, and 0 is defined as a null pointer constant in the C ++ standard, so I think it is more correct to use 0 rather than NULL.

If you don't want to include those headers just use a deviation permit (something like NULL is obsolete because C++11 has the far safer nulllptr keyword).

misra cpp
Posts: 145
Joined: Mon Jun 02, 2008 1:55 pm
Company: MISRA

Re: Rule 4-10-2 NULL is not a c++ standard

Postby misra cpp » Tue Jan 24, 2017 10:13 am

Whilst agreeing with dg1980, an alternative approach if you know you do not want to include any of the C headers that include the NULL definition is to add the #define NULL 0 declaration yourself (making any required deviations necessary for use of #define other than as a guard)

The aim is to try and make it clear to the programmer (and particularly any subsequent maintainers) the difference between a value that is a pointer and an integer, hence not allowing pointer = 0;
Posted by and on behalf of
the MISRA C++ Working Group


Return to “6.4 Standard conversions (C++)”

Who is online

Users browsing this forum: No registered users and 1 guest