Any reason to use hexadecimal notation for null pointers?

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

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

+4
source share
8 answers

Personally, I print 0x0 to the log file [*]. Someday, when someone comes to parsing a file, more uniform data is better. I don't find 0x0 hard to read, so it seems silly to have a special case in the writer code and another special case in the reader code, without any benefit that I can think of.

0x0 is preferred for 0 for grepping the log for NULL too: it allows you to understand that you must be grepping for )0 or something funny.

I would not write 0x0 for a null pointer constant in C or C ++. I write non-empty addresses so incredibly rare that there is nothing for zeros to be uniform. I assume that if I defined a group of constants to represent the memory card of any device, and the zero address was significant in this memory card, then I could write it 0x0 in this context.

[*] Or maybe 0x00000000 . I like 32-bit pointers, which will be printed 8 characters long, because when I read / remember the pointer, I start pairwise on the left. If you get 7 characters, in the end I'm terribly confused ;-). 64-bit pointers, it doesn’t matter, because I can’t remember a number that is long anyway ...

+1
source

In C or C ++, you should use the standard %p formatting code, which then makes your pointers look like everyone else.

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

+2
source

Using the notation 0x0 (IMO), it becomes clear that it refers to the address (even if it is not an internal representation of a null pointer). (In the code itself, I prefer to use the NULL macro, but it looks like you're talking specifically about debugging spew.)

It gives some context, just as I prefer to use '\0' for the NUL terminator.

This is a stylistic preference, however, do what you like (and your colleagues).

+2
source

In the end, all the positive zeros.

+1
source

Exists: you can always convert them back to number (0) without any extra effort. And the only drawback is readability.

+1
source

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

Aside: in C, the null pointer constant is a somewhat strange construct; the compiler recognizes (SomeType *) 0 as a "null pointer", even if the internal representation on some machine may differ from the numerical value 0. This is more like NULL in SQL, rather than a "real" pointer value. In practice, all the machines that I know model a null pointer as the address "0".

+1
source

I am sure that the hexadecimal notation is the result of memory layout. The memory is aligned word by word when the word is 32 bits, if you are on a 32-bit processor. These words are segmented into pages that are located in page tables, etc. Etc. Hex is the only way to understand these measures (if you don't like using your calculator).

+1
source

My opinion, for readability, think about it, if you looked at 0, what does it mean if it means its 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 an agnostic of the language, 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 the C-FAQ, for example, in section 5 for NULL pointers, specifically 5.4 , 5.5 , 5.6, and 5.7 , to give you an idea of ​​this.

In a nutshell, the use and designation of null pointers depends on

  • What language is used?
  • Semantics and syntax of language specifications.
  • What type of compiler?
  • Platform type, in terms of memory access, processor, bit ...

Hope this helps, Regards, Tom.

+1
source

Source: https://habr.com/ru/post/1299304/


All Articles