In C, what does NULL look like, byte by byte in memory?
Okay, results are in!
Not much love for 0x55555555 huh?
Well first let& #39;s see what the C standard says.
Hm, so NULL& #39;s *value* must compare equal to 0. Case closed?
Not much love for 0x55555555 huh?
Well first let& #39;s see what the C standard says.
Hm, so NULL& #39;s *value* must compare equal to 0. Case closed?
No, not case closed. We& #39;re after the byte-by-byte layout in memory. That& #39;s called a representation.
So the value must compare equal to 0, but the representation is unspecified.
That means it& #39;s up to your implementation.
So the value must compare equal to 0, but the representation is unspecified.
That means it& #39;s up to your implementation.
What& #39;s an implementation? Usually a compiler.
But nothing says C must be compiled. C interpreters do exist!
Compilers are my jam though, so let& #39;s look at those.
But nothing says C must be compiled. C interpreters do exist!
Compilers are my jam though, so let& #39;s look at those.
We have a more precise question now:
How does a compiler decide on a representation for a null pointer?
Well that& #39;s usually determined by the ABI for your platform.
Sometimes an ABI is well defined, sometimes it& #39;s an ad-hoc mess adding on whatever happens to be there already.
How does a compiler decide on a representation for a null pointer?
Well that& #39;s usually determined by the ABI for your platform.
Sometimes an ABI is well defined, sometimes it& #39;s an ad-hoc mess adding on whatever happens to be there already.
Just like in web development har har
Okay so a non-zero null pointer representation is theoretically possible.
But does it happen in practice?
You bet! Stuff is codified in the standard because it actually happens.
Like 0xB00000000000 on the CDC Cyber 180 (great name), and 06000 on a Honeywell mainframe.
But does it happen in practice?
You bet! Stuff is codified in the standard because it actually happens.
Like 0xB00000000000 on the CDC Cyber 180 (great name), and 06000 on a Honeywell mainframe.
The clc FAQ also has an example of a machine where
sizeof (char *) > sizeof (int *):
http://c-faq.com/null/machexamp.html
That">https://c-faq.com/null/mach... happens because it& #39;s a word addressed machine, and it takes literally extra lines on the bus to address something within a word. So it& #39;s a wider memory bus.
sizeof (char *) > sizeof (int *):
http://c-faq.com/null/machexamp.html
That">https://c-faq.com/null/mach... happens because it& #39;s a word addressed machine, and it takes literally extra lines on the bus to address something within a word. So it& #39;s a wider memory bus.
But Kate, those are all stupid old machines. This doesn& #39;t apply to everybody& #39;s favourite and most secure CPU, x86. Right?
Well hey, you can totally violate an ABI if nobody catches you. I& #39;m not the police.
Well hey, you can totally violate an ABI if nobody catches you. I& #39;m not the police.
And that brings us to the TenDRA compiler, which can use 0x55555555 for its null pointer representation, if you like.
So here& #39;s an implementation of C compiling to x86 where the value of NULL is 0, but the underlying byte-by-byte representation is 0x55555555.
So here& #39;s an implementation of C compiling to x86 where the value of NULL is 0, but the underlying byte-by-byte representation is 0x55555555.