views:

205

answers:

8

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?

+1  A: 

It's all positive zero in the end.

Ignacio Vazquez-Abrams
+2  A: 

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.

unwind
+1  A: 

There is: You can always convert them back to a number (0), with no additional effort. And the only disadvantage is readability.

alemjerus
+1  A: 

There is no reason to prefer (SomeType*)0x0 to (SomeType*)0.

As an aside: In C, the null pointer constant is a somewhat strange construct; the compiler recognizes (SomeType*)0 as "the null pointer", even if the internal representation on some machine might differ from the numerical value 0. It is more like NULL in SQL -- not a "real" pointer value. In practice, all machines I know of model the null pointer as the "0" address.

mfx
http://c-faq.com/null/machexamp.html
jamesdlin
+2  A: 

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).

jamesdlin
+1  A: 

I am pretty sure the hex notation is a result of the layout of memory. Memory is word aligned, where a word is 32 bits if you are on a 32 bit processor. These words are segmented into pages, which are arranged in page tables, etc. etc. Hex notation is the only way to make sense of this arrangements (unless you really like using your calculator).

Bear
Sure, hex for non-null addresses makes a lot of sense. I ask about null pointers solely.
sharptooth
Why would you make an exception for null?
Bear
I guess it's the result of reading "Use literal zero to indicate a null pointer in C++" so many times.
sharptooth
+1  A: 

Personally, I'd print 0x0 to the log file[*]. Some day when someone comes to parse the file automatically, the more uniform the data is the better. I don't find 0x0 difficult to read, so it seems silly to have a special case in the writer code, and another special case in the reader code, for no benefit that I can think of.

0x0 is preferable to 0 for grepping the log for NULLs, too: saves you having to figure out that you should be grepping for )0 or something funny.

I wouldn't write 0x0 for a null pointer constant in C or C++, though. I write non-null addresses so unbelievably rarely that there's nothing for the nulls to be uniform with. I guess if I was defining a bunch of constants to represent the memory map of some device, and the zero address was significant in that memory map, then I might write it 0x0 in that context.

[*] Or perhaps 0x00000000. I like 32-bit pointers to be printed 8 chars long, because when I read/remember a pointer I start out in pairs from the left. If it turns out to have 7 chars, I get horribly confused at the end ;-). 64-bit pointers it doesn't matter, because I can't remember a number that long anyway...

Steve Jessop
+1  A: 

My opinion, is for readability, think about it, if you were to look at 0, what does that mean, does that mean its a unsigned integer, or if it was 0x0, then instinctively, it has something to do with binary notation, more likely platform dependent.

Since the tag is language agnostic, and the word 'null pointer', in Delphi/Object Pascal, it is 'nil', in C#, it is 'null', in C/C++ it is 'NULL'.

Look at for example in the C-FAQ, in Section 5 on NULL pointers, specifically, 5.4, 5.5, 5.6 and 5.7 to give you an insight into this.

In a nutshell, the usage and notation of null pointers is dependent on

  • What language is used?
  • Semantics and syntax of the language specifications.
  • What type of compiler?
  • Type of platform, in terms of how memory is accessed, the processor, bits...

Hope this helps, Best regards, Tom.

tommieb75
In fact we have a C-style cast printed prior to the address - something line (IInterface*)0 - so that it's clear what the type of argument is. Otherwise when you have a method with three pointers they are all some unpredictable hex values.
sharptooth