Hi Branden, On 8/3/22 17:58, G. Branden Robinson wrote:
At 2022-08-03T01:54:18+0200, Alejandro Colomar wrote:Could you show an example? I'm curious.Sure. Let me quote Andrew Koenig, _C Traps and Pitfalls_, Addison-Wesley, 1989. "I once talked to someone who was writing a C program to run stand-alone in a microprocessor. When this machine was switched on, the hardware would call the subroutine whose address was stored in location zero. In order to simulate turning power on, we had to devise a C statement that would call this subroutine explicitly. After some thought, we came up with the following. (*(void(*)())0)();
That piece of code is beautiful.
Expressions like these strike terror into the hearts of C programmers." I, too, initially viewed it with dismay. But I stared at it for a while, and once I parsed it, I cackled with pleasure.[1] However, my pleasure would turn to anger if some jackanapes stuck a comment above it reading as follows. /* You are not expected to understand this. */
:)
This comment from the V6 Unix kernel is famous, more famous unfortunately than the fact that Dennis Ritchie himself repented of this comment, and explained himself beautifully. https://www.bell-labs.com/usr/dmr/www/odd.html Regards, Branden [1] Here, at least, the addressing mode of the resulting instruction _is_ more or less visible in the C statement. In this instance I don't think "NULL" would make it clearer, and I would come down on the McIlroy side of the argument. But this circumstance has nearly vanished from the domain of practical application. Thanks largely to C and the generations of programmers it has fascinated with its charming and attractive footguns, null pointers have been chased with such neurotic compulsion that it is common these days to program the MMU to raise an invalid address fault on any access to the first page of memory. Although I guess as recently as 2009, Linux didn't do this. https://lwn.net/Articles/342330/ On 64-bit architectures where the backing of the address space by actual storage is once again sparse, I don't know of a reason to use the page at the bottom of memory for storage.
I think this is one of those cases where the all-0s bit pattern doesn't necessarily correspond to a NULL pointer. That's using a pointer to something that exists and can be dereferenced, and happens to be at address 0. So, I agree with you that using the address 0 in this case makes much more sense than dereferencing the NULL pointer, which is supposed to be an invalid pointer, one that doesn't point to anything.
Cheers, Alex -- Alejandro Colomar <http://www.alejandro-colomar.es/>
OpenPGP_signature
Description: OpenPGP digital signature