Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any reason to use hex notation for null pointers?

I'm currently improving the part of our COM component that logs all external calls into a file. For pointers we write something like (IInterface*)0x12345678 with the value being equal to the actual address.

Currently no difference is made for null pointers - they are displayed as 0x0 which IMO is suboptimal and inelegant. Changing this behaviour is not a problem at all. But first I'd like to know - is there any real advantage in representing null pointers in hex?

like image 670
sharptooth Avatar asked Dec 29 '22 04:12

sharptooth


2 Answers

In C or C++, you should be able to use the standard %p formatting code, which will then make your pointers look like everybody else's.

I'm not sure how null pointers are formatted in Win32 by %p, on Linux I think you get "null" or something similar.

like image 132
unwind Avatar answered Jan 09 '23 13:01

unwind


Using the notation 0x0 (IMO) makes it clearer that it's referring to an address (even if it's not the internal representation of the null pointer). (In actual code, I prefer would using the NULL macro, though, but it sounds like you're talking specifically about debugging spew.)

It gives some context, just like I prefer using '\0' for the NUL-terminator.

It's a stylistic preference, though, so do what appeals to you (and to your colleagues).

like image 32
jamesdlin Avatar answered Jan 09 '23 13:01

jamesdlin