6

in g++, NULL is defined as __null, in 64-bit case, __null is 8 bytes. such as:

printf("sizeof(__null):%d, sizeof(0):%d\n", sizeof(__null), sizeof(0));
sizeof(__null):8, sizeof(0):4

however, where is __null defined?

wenlujon
  • 673
  • 1
  • 6
  • 16

1 Answers1

7

The implementation of __null is as a G++ internal. You won't find it in a header file or anything like that. You can find some explanation of the logic here but the basic idea is that it's the simplest way to ensure NULL is seen as a pointer first.

Basically, the internal does what you would naively expect reinterpret_cast<void *>(0) to do.

Jimmy T.
  • 4,033
  • 2
  • 22
  • 38
David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • is there any way to detect the type of it? – wenlujon Jan 09 '12 at 06:28
  • 1
    Its type is 'magic', depending on context. That's the reason G++ had to implement it as an internal. No regular type provides the precisely-correct semantics. It acts roughly like 'void *', but not exactly. – David Schwartz Jan 09 '12 at 06:31
  • Your link to "the logic" is now broken. BTW, in the context of `struct S{}s=__null;`, `__null` appears to behave as an `int`: the error emitted is "conversion from `int` to non-scalar type `S` requested". – Ruslan May 11 '20 at 10:42