Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instead of NULL, should I write `0x0` or `0`?

Tags:

c++

null

I know that you are supposed to use 0 instead of NULL in c++ (even though NULL is defined as 0 in C++ most of the time).

Recently I came across some code where 0x0 was used instead, though.

What is the difference?

like image 737
moka Avatar asked Nov 19 '09 17:11

moka


People also ask

Can I use 0 instead of null?

Null Values can be replaced in SQL by using UPDATE, SET, and WHERE to search a column in a table for nulls and replace them. In the example above it replaces them with 0.

Is 0x0 a valid address?

Since the zero page can be mapped and therefore 0x0 is a valid address, the zero bit pattern should not be used for the null pointer.

Is 0 the same as null in C?

The C standard defines that 0 is typecast to (void *) is both a null pointer and a null pointer constant.

What can I use instead of null in C?

Instead of NULL , they use nullptr , a new keyword introduced in C++11. Like NULL , nullptr implicitly converts to T* for any type T . Unlike NULL , nullptr is not an integer so it cannot call the wrong overload. Unlike NULL , nullptr has its own type, nullptr_t , so the compiler makes correct type deductions.


2 Answers

0x0 is just 0 written in hexadecimal notation. There is no difference between the two:

016 = 010 :)

NULL is usually #defined to 0 somewhere and does the same thing.

like image 93
mmx Avatar answered Sep 22 '22 06:09

mmx


There is no difference. In C, NULL is often defined to be (void *)0, but in C++ that's not allowed. A few ancient compilers got this wrong, but they really are ancient.

IMO, it's better to use NULL, as it portrays the intent more clearly, and gives you a nice, easy symbol to S&R when your compiler gets updated to C++ 0x, which will include nullptr.

like image 24
Jerry Coffin Avatar answered Sep 20 '22 06:09

Jerry Coffin